作者kuramax77 (熊仔)
看板Web_Design
標題[問題] jsp的寄信功能
時間Tue Sep 7 00:23:19 2010
因為想利用jsp網頁+smtp的功能來實作
但因為smtp server有限制port,所以想請問一下以下程式碼該如何改才好
ex. smtp:mail.cycu.edu.tw
port:995
//===================================================
InternetAddress[] address = null;
request.setCharacterEncoding("MS950");
String mailserver = "mail.cycu.edu.tw";
String From = request.getParameter("From");
String to = request.getParameter("To");
String Subject = request.getParameter("Subject");
String messageText = request.getParameter("Message");
boolean sessionDebug = false;
try {
// 設定所要用的Mail 伺服器和所使用的傳送協定
java.util.Properties props = System.getProperties();
props.put("mail.host",mailserver);
props.put("mail.transport.protocol","smtp");
// 產生新的Session 服務
javax.mail.Session mailSession =
javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
// 設定傳送郵件的發信人
msg.setFrom(new InternetAddress(From));
// 設定傳送郵件至收信人的信箱
address = InternetAddress.parse(to,false);
msg.setRecipients(Message.RecipientType.TO, address);
// 設定信中的主題
msg.setSubject(Subject);
// 設定送信的時間
msg.setSentDate(new Date());
// 設定傳送信的MIME Type
msg.setText(messageText);
// 送信
Transport.send(msg);
out.println("郵件己順利傳送");
}
catch (MessagingException mex) {
mex.printStackTrace();
}
//===================================================================
在此先謝謝各位大大幫忙了!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.43.225.248
→ adahsu:您沒有指定 port 啊... 09/07 09:23
→ kuramax77:當我指定成mail.cycu.edu.tw:995這樣子的時候還是不行 09/08 15:50
→ kuramax77:是我指定的方式不對嗎@@? 09/08 15:50
→ adahsu:不對!我記得是分開設定的... 請找一下 javamail docs 09/09 12:54