2010年12月8日 星期三

[ Java 代碼範本 ] 使用 JavaMail 發送/接收 Gmail


參考至 這裡
前言 :
Here’s two examples to show how to use JavaMail API method to send an email via Gmail SMTP server using both TLS and SSL connection.

JavaMail – GMail via TLS :
Sending an email via Gmail SMTP server using TLS connection. 範例代碼如下 :
- GmailApp1.java : Using TLS to send Gmail
  1. package john.mail;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import javax.mail.Authenticator;  
  6. import javax.mail.Message;  
  7. import javax.mail.MessagingException;  
  8. import javax.mail.PasswordAuthentication;  
  9. import javax.mail.Session;  
  10. import javax.mail.Transport;  
  11. import javax.mail.internet.InternetAddress;  
  12. import javax.mail.internet.MimeMessage;  
  13.   
  14. public class GmailApp1 {  
  15.     public static void main(String args[]) {  
  16.         String host = "smtp.gmail.com";  
  17.         int port = 587;  
  18.         final String username = "puremonkey2007@gmail.com";  
  19.         final String password = "your password";  
  20.   
  21.         Properties props = new Properties();  
  22.         props.put("mail.smtp.host", host);  
  23.         props.put("mail.smtp.auth""true");  
  24.         props.put("mail.smtp.starttls.enable""true");  
  25.         props.put("mail.smtp.port", port);  
  26.           
  27.         Session session = Session.getInstance(props,new Authenticator(){  
  28.               protected PasswordAuthentication getPasswordAuthentication() {  
  29.                   return new PasswordAuthentication(username, password);  
  30.               }} );  
  31.            
  32.         try {  
  33.   
  34.         Message message = new MimeMessage(session);  
  35.         message.setFrom(new InternetAddress("puremonkey2007@gmail.com"));  
  36.         message.setRecipients(Message.RecipientType.TO,   
  37.                         InternetAddress.parse("john_k_lee@trend.com.tw"));  
  38.         message.setSubject("Testing Subject");  
  39.         message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");  
  40.   
  41.         Transport transport = session.getTransport("smtp");  
  42.         transport.connect(host, port, username, password);  
  43.   
  44.         Transport.send(message);  
  45.   
  46.         System.out.println("Done");  
  47.   
  48.         } catch (MessagingException e) {  
  49.             throw new RuntimeException(e);  
  50.         }  
  51.     }  
  52. }  

JavaMail – GMail via SSL :
Sending an email via Gmail SMTP server using SSL connection. 範例代碼如下 :
- GmailApp2.java : Send Gmail through SSL
  1. package john.mail;  
  2.   
  3. import java.util.Properties;  
  4. import javax.mail.Authenticator;  
  5. import javax.mail.Message;  
  6. import javax.mail.MessagingException;  
  7. import javax.mail.PasswordAuthentication;  
  8. import javax.mail.Session;  
  9. import javax.mail.Transport;  
  10. import javax.mail.internet.InternetAddress;  
  11. import javax.mail.internet.MimeMessage;  
  12.   
  13. public class GmailApp2 {  
  14.   
  15.     public static void main(String[] args) {  
  16.         final String username = "puremonkey2007@gmail.com";  
  17.         final String password = "your password";  
  18.   
  19.         Properties props = new Properties();  
  20.         props.put("mail.smtp.host""smtp.gmail.com");  
  21.         props.put("mail.smtp.socketFactory.port""465");  
  22.         props.put("mail.smtp.socketFactory.class",  
  23.                 "javax.net.ssl.SSLSocketFactory");  
  24.         props.put("mail.smtp.auth""true");  
  25.         props.put("mail.smtp.port""465");  
  26.   
  27.         Session session = Session.getInstance(props, new Authenticator() {  
  28.             protected PasswordAuthentication getPasswordAuthentication() {  
  29.                 return new PasswordAuthentication(username, password);  
  30.             }  
  31.         });  
  32.   
  33.         try {  
  34.   
  35.             Message message = new MimeMessage(session);  
  36.             message.setFrom(new InternetAddress("puremonkey2007@gmail.com.tw"));  
  37.             message.setRecipients(Message.RecipientType.TO, InternetAddress  
  38.                     .parse("john_k_lee@trend.com.tw"));  
  39.             message.setSubject("Testing Subject");  
  40.             message  
  41.                     .setText("Dear Mail Crawler,\n\n No spam to my email, please!");  
  42.   
  43.             Transport.send(message);  
  44.             System.out.println("Done");  
  45.   
  46.         } catch (MessagingException e) {  
  47.             throw new RuntimeException(e);  
  48.         }  
  49.     }  
  50. }  

補充說明 :
JavaMail API – Sending email via Gmail SMTP example
利用JavaMail 收/發 Gmail 郵件(SSL)
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMail如何收取Gmail邮件以及发送邮件的代码...
This message was edited 6 times. Last update was at 08/12/2010 17:02:29

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...