`
ssxxjjii
  • 浏览: 934784 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaMail读发邮件

阅读更多

SMTP(Simple Mail Transfer Protocol),即简单邮件传输协议,它定义了发送电子邮件的机制。在 JavaMail API 环境中,基于 JavaMail 的程序将和您的公司或因特网服务供应商的(Internet Service Provider's,ISP's)SMTP 服务器通信。SMTP 服务器可将消息中转至接收方 SMTP 服务器,以便最终让用户经由 POP 或 IMAP 获得。这不是要求 SMTP 服务器成为开放的中继,尽管 SMTP 服务器支持身份验证,不过还是得确保它的配置正确。像配置服务器来中继消息或添加删除邮件账号这类任务的实现,JavaMail API 中并不支持。 

  POP(Post Office Protocol),即邮局协议。目前用的是版本3,所以人们通常将它称为 POP3,RFC 1939 定义了这个协议。POP 和SMTP一样,也是一种机制,Internet上大多数人通过它得到邮件。该协议规定每个用户只能有一个邮箱的支持。这就是它所能做的,而这也造成了许多混淆。使用 POP 时,用户熟悉的许多性能并不是由 POP 协议支持的,如查看有几封新邮件消息这一性能。这些性能内建于如 Eudora 或 Microsoft Outlook 之类的程序中,它们能记住一些事,诸如最近一次收到的邮件,还能计算出有多少是新的。所以当使用 JavaMail API 时,如果读者想要这类信息,就只能由自己来计算了。 

  IMAP(Internet Message Access Protocol),即Internet消息访问协议,是更高级的用于接收消息的协议,在 RFC 2060 中有它的定义。目前使用的IMAP版本为4,人们习惯将它称为 IMAP4。在用到 IMAP 时,邮件服务器必需支持这个协议。不能仅仅把使用 POP 的程序用于 IMAP,并指望它支持 IMAP 所有性能。假设邮件服务器支持 IMAP,基于 JavaMail 的程序可以利用这种情况--用户在服务器上可以有多个文件夹(folder),并且这些文件夹可以被多个用户共享。 
  因为有这一更高级的性能,您也许会认为所有用户都会使用 IMAP。事实并不是这样。要求服务器接收新消息,在用户请求时发送到用户手中,还要在每个用户的多个文件夹中维护消息。这样虽然能将消息集中备份,但随着用户长期的邮件夹越来越大,到磁盘空间耗尽时,每个用户都会受到损失。使用 POP,就能卸载邮件服务器上保存的消息了。 

  MIME(Multipurpose Internet Mail Extensions),即多用途Internet邮件扩展标准。它不是邮件传输协议,而是对传输内容的消息、附件及其它的内容定义了格式。这里有很多不同的RFC(Requirement of Comment)文档:RFC 822、RFC 2045、RFC 2046 和 RFC 2047。作为一个 JavaMail API 的用户,您通常不必对这些格式操心。无论如何,一定存在这些格式而且程序会用到它。 

二、例子 

Java代码 
  1. import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.security.Security;  
  7. import java.util.Date;  
  8. import java.util.Enumeration;  
  9. import java.util.Properties;  
  10. import java.util.Vector;  
  11.   
  12. import javax.activation.DataHandler;  
  13. import javax.activation.FileDataSource;  
  14. import javax.mail.Authenticator;  
  15. import javax.mail.BodyPart;  
  16. import javax.mail.FetchProfile;  
  17. import javax.mail.Folder;  
  18. import javax.mail.Message;  
  19. import javax.mail.MessagingException;  
  20. import javax.mail.Multipart;  
  21. import javax.mail.Part;  
  22. import javax.mail.PasswordAuthentication;  
  23. import javax.mail.Session;  
  24. import javax.mail.Store;  
  25. import javax.mail.Transport;  
  26. import javax.mail.URLName;  
  27. import javax.mail.internet.AddressException;  
  28. import javax.mail.internet.InternetAddress;  
  29. import javax.mail.internet.MimeBodyPart;  
  30. import javax.mail.internet.MimeMessage;  
  31. import javax.mail.internet.MimeMultipart;  
  32. import javax.mail.internet.MimeUtility;  
  33.   
  34. /** 
  35.  * 使用Gmail发送邮件 
  36.  */  
  37. public class GmailSenderAndFetch {  
  38.   
  39.     private static String messageContentMimeType = "text/html; charset=gb2312";  
  40.   
  41.     private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";  
  42.   
  43.     public static Properties getProperties() {  
  44.         Properties props = System.getProperties();  
  45.         props.setProperty("mail.smtp.host""smtp.gmail.com");  
  46.         // Gmail提供的POP3和SMTP是使用安全套接字层SSL的  
  47.         props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);  
  48.         props.setProperty("mail.smtp.socketFactory.fallback""false");  
  49.         props.setProperty("mail.smtp.port""465");  
  50.         props.setProperty("mail.smtp.socketFactory.port""465");  
  51.   
  52.         props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);  
  53.         props.setProperty("mail.imap.socketFactory.fallback""false");  
  54.         props.setProperty("mail.imap.port""993");  
  55.         props.setProperty("mail.imap.socketFactory.port""993");  
  56.   
  57.         props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);  
  58.         props.setProperty("mail.pop3.socketFactory.fallback""false");  
  59.         props.setProperty("mail.pop3.port""995");  
  60.         props.setProperty("mail.pop3.socketFactory.port""995");  
  61.   
  62.         props.put("mail.smtp.auth""true");  
  63.         return props;  
  64.     }  
  65.   
  66.     /** 
  67.      * 构建邮件 
  68.      *  
  69.      * @param username 
  70.      * @param password 
  71.      * @param receiver 
  72.      * @return 
  73.      * @throws AddressException 
  74.      * @throws MessagingException 
  75.      */  
  76.     @SuppressWarnings( { "unchecked""serial" })  
  77.     public static Message buildMimeMessage(final String username,  
  78.             final String password, String receiver) throws AddressException,  
  79.             MessagingException {  
  80.         Session session = Session.getDefaultInstance(getProperties(),  
  81.                 new Authenticator() {  
  82.                     protected PasswordAuthentication getPasswordAuthentication() {  
  83.                         return new PasswordAuthentication(username, password);  
  84.                     }  
  85.                 });  
  86.   
  87.         Message msg = new MimeMessage(session);  
  88.           
  89.         //鉴别发送者,您可以使用setFrom()和setReplyTo()方法。  
  90.         //msg.setFrom(new InternetAddress("[发件人]"));  
  91.         msg.addFrom(InternetAddress.parse("[发件人]"));//地址来源,没作用?  
  92.         msg.setReplyTo(InternetAddress.parse("[回复时收件人]"));//回复时用的地址  
  93.         //消息接收者  
  94.         msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(  
  95.                 receiver, false));  
  96.         msg.setSubject("JavaMail邮件发送");  
  97.         msg.setSentDate(new Date());  
  98.   
  99.         String content = "How are you!这是一个测试!";  
  100.         // 邮件内容数据(Content)  
  101.         msg.setContent(buildMimeMultipart(content, new Vector() {  
  102.             {  
  103.                 add("D:/uploadDir/test.txt");  
  104.             }  
  105.         }));  
  106.   
  107.         return msg;  
  108.     }  
  109.   
  110.     /** 
  111.      * 构建邮件的正文和附件 
  112.      *  
  113.      * @param msgContent 
  114.      * @param attachedFileList 
  115.      * @return 
  116.      * @throws MessagingException 
  117.      */  
  118.     public static Multipart buildMimeMultipart(String msgContent,  
  119.             Vector attachedFileList) throws MessagingException {  
  120.         Multipart mPart = new MimeMultipart();// 多部分实现  
  121.   
  122.         // 邮件正文  
  123.         MimeBodyPart mBodyContent = new MimeBodyPart();// MIME邮件段体  
  124.         if (msgContent != null) {  
  125.             mBodyContent.setContent(msgContent, messageContentMimeType);  
  126.         } else {  
  127.             mBodyContent.setContent("", messageContentMimeType);  
  128.         }  
  129.         mPart.addBodyPart(mBodyContent);  
  130.   
  131.         // 附件  
  132.         String file;  
  133.         String fileName;  
  134.         if (attachedFileList != null) {  
  135.             for (Enumeration fileList = attachedFileList.elements(); fileList  
  136.                     .hasMoreElements();) {  
  137.                 file = (String) fileList.nextElement();  
  138.                 fileName = file.substring(file.lastIndexOf("/") + 1);  
  139.                 MimeBodyPart mBodyPart = new MimeBodyPart();  
  140.                 //远程资源  
  141.                 //URLDataSource uds=new URLDataSource(http://www.iteye.com/logo.gif);  
  142.                 FileDataSource fds = new FileDataSource(file);  
  143.                 mBodyPart.setDataHandler(new DataHandler(fds));  
  144.                 mBodyPart.setFileName(fileName);  
  145.                 mPart.addBodyPart(mBodyPart);  
  146.             }  
  147.         }  
  148.   
  149.         return mPart;  
  150.     }  
  151.   
  152.     /** 
  153.      * 字串解码 
  154.      *  
  155.      * @param text 
  156.      * @return 
  157.      * @throws UnsupportedEncodingException 
  158.      */  
  159.     protected static String decodeText(String text)  
  160. padding-right: 0px; padding-left: 10px; font-size: 1em; padding-bottom: 0px; margin: 0px 0px 0px 38px; border-left: #d1d7dc 1px solid; line-height:
    分享到:
    评论

相关推荐

Global site tag (gtag.js) - Google Analytics