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
本主題所含的標籤
無標籤
作者 利用 Gmail 發信
kalin





發文: 26
積分: 0
於 2006-07-02 00:08 user profilesend a private message to usersend email to kalinreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在 http://blog.iyi.cn/user/david/archives/2004/12/160.html 看到用 gmail 發送信件的原始碼:

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
import java.security.Security;
import java.util.Date;
import java.util.Properties;
 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class GmailSender {
 
 public static void main(String[] args) throws AddressException, MessagingException {
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  // Get a Properties object
  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");
  final String username = "郵件帳號";
  final String password = "郵件密碼";
  Session session = Session.getDefaultInstance(props, new Authenticator(){
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(username, password);
      }});
 
       // -- Create a new message --
  Message msg = new MimeMessage(session);
 
  // -- Set the FROM and TO fields --
  msg.setFrom(new InternetAddress(username + "@gmail.com"));
  msg.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse("郵件信箱",false));
  msg.setSubject("Hello");
  msg.setText("How are you");
  msg.setSentDate(new Date());
  Transport.send(msg);
  
  System.out.println("Message sent.");
 }
}


可是我發覺只要把 Message.RecipientType.TO 改成 Message.RecipientType.BCC 雖然仍是正常的跑完程式, 但是就收不到信了, 請問是不是哪裡出錯了呢?


reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
ahcheong





發文: 19
積分: 0
於 2007-03-17 10:29 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
我用了以上的code出现了以下的问题

invalid entry for security.provider.3
Exception in thread "main" java.lang.NoClassDefFoundError: javax/net/SocketFactory
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:891)
at java.lang.ClassLoader.loadClass(ClassLoader.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:150)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at NewEmpty.main(NewEmpty.java:45)
Java Result: 1


reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
cd109





發文: 8
積分: 0
於 2007-03-19 09:56 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
你好,我依照您的範例,添加下列的jar檔案,是可以正常執行的~~
Message.RecipientType.TO 與 Message.RecipientType.BCC 均可寄出~~

http://java.sun.con/products/javamail/
http://java.sun.com/beans/glasgow/jaf.html


reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
akuto





發文: 5
積分: 0
於 2007-10-20 14:58 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
我也嘗試用以上的原始碼, 可是有以下問題, 是否socket出了什麼問題? 謝謝~

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
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
        java.net.ConnectException: Connection timed out: connect
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:37
0)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at GmailSender.main(GmailSender.java:45)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
        at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source
)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
        ... 7 more


reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2007-10-20 23:56 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
是不是防火牆?
我記得我利用類似code寄信沒問題

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:利用 Gmail 發信 [Re:kalin]
akuto





發文: 5
積分: 0
於 2007-10-22 22:41 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
原來真的是防火牆的問題, 謝謝~

reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
ivyislai





發文: 1
積分: 0
於 2011-09-02 10:53 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
整包測試的代碼
歡迎參考

http://www.box.net/shared/nb8ea1jq48fu842jjof2


ivyislai edited on 2011-09-06 09:28
reply to postreply to post
作者 Re:利用 Gmail 發信 [Re:kalin]
opalasd





發文: 5
積分: 0
於 2013-04-09 16:30 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
請問如果要在寄件內文 自己塞入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