JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » EJB / 其它Java EE 討論區 » JavaMail  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 JavaMail ~使用Gmail發信?
sunnyng236





發文: 2
積分: 0
於 2009-09-08 23:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我使用Gmail還是無法成功的發信,以下是我的code,幫忙看一下問題所在,謝謝!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<%@ page import="javax.mail.*" %>                        <==包含在mail.jar
<%@ page import="javax.mail.internet.*" %>            <==包含在mail.jar
<%@ page import="javax.activation.*" %>                <==包含在activation.jar
<%@ page import="java.util.*,java.io.*" %>
<%@ page import ="java.security.*" %>
<%@ page import ="java.util.Date" %>
<%@ page import ="java.util.Properties" %>
<%@ page import ="javax.mail.Authenticator" %>
<%@ page import ="javax.mail.Message" %>
<%@ page import ="javax.mail.MessagingException" %>
<%@ page import ="javax.mail.PasswordAuthentication" %>
<%@ page import ="javax.mail.Session" %>
<%@ page import ="javax.mail.Transport" %>
<%@ page import ="javax.mail.internet.AddressException" %>
<%@ page import ="javax.mail.internet.InternetAddress" %>
<%@ page import ="javax.mail.internet.MimeMessage" %>
<%@ page contentType="text/html;charset=big5" %>
 
<html>
<head>
  <title>JavaMail.jsp</title>
</head>
<body>
 <h2>利用JavaMail來傳送電子郵件 </h2>
<%
    InternetAddress[] address = null;
    request.setCharacterEncoding("big5");
    String mailserver   = "mail.dwu.edu.tw";                           <=此處所設必須和寄件人的信箱同一台伺服器,
    String From         = request.getParameter("From");                    並且必須考慮伺服器是否會mail-rely
    String to           = request.getParameter("To");
    String Subject      = request.getParameter("Subject");
    String messageText  = request.getParameter("Message");
    String username    = "XXXX";
    String password    = "XXX";
    boolean sessionDebug = false;
    
try {
  // 設定所要用的Mail 伺服器和所使用的傳送協定
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
 
  // 產生新的Session 服務
    Session session = Session.getDefaultInstance(props, new Authenticator(){
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }});
    Message msg = new MimeMessage(session);
  // 設定傳送郵件的發信人
  msg.setFrom(new InternetAddress(username + "@gmail.com"));
  // 設定傳送郵件至收信人的信箱
  address = InternetAddress.parse("XXX@XXX",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();
    }
%>
 </body>
</html>


reply to postreply to post
» JWorld@TW »  EJB / 其它Java EE 討論區 » JavaMail

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8