JavaMail发送邮件(含附件)的例子

转:http://blog.sina.com.cn/s/blog_3f0cd39a010006pa.html

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mogoko.common.email;  
  2.   
  3. import javax.mail.Session;  
  4. import javax.mail.MessagingException;  
  5. import javax.mail.Multipart;  
  6. import javax.mail.Transport;  
  7. import javax.mail.internet.InternetAddress;  
  8. import javax.mail.internet.MimeMessage;  
  9. import javax.mail.internet.MimeBodyPart;  
  10. import javax.mail.internet.MimeMultipart;  
  11. import javax.activation.FileDataSource;  
  12. import javax.activation.DataHandler;  
  13.   
  14.   
  15.   
  16.   
  17. public class SendAttachMail {  
  18. public static void sendMessage(String smtpHost,String from, String to,String subject, String messageText,String fileName)  
  19.     throws MessagingException {  
  20.   
  21. // Step 1: Configure the mail session  
  22. java.util.Properties props = new java.util.Properties();  
  23. props.setProperty("mail.smtp.auth""true"); //指定是否需要SMTP验证  
  24. props.setProperty("mail.smtp.host", smtpHost); //指定SMTP服务器  
  25. props.put("mail.transport.protocol""smtp"); //指定传输协议  
  26.   
  27.   
  28. Session mailSession = Session.getDefaultInstance(props);  
  29. mailSession.setDebug(false); //是否在控制台显示debug信息  
  30.   
  31. // Step 2: Construct the message  
  32. System.out.println("Constructing message - from=" + from + " to=" +to);  
  33.   
  34. InternetAddress fromAddress = new InternetAddress(from); //From Mail  
  35. InternetAddress toAddress = new InternetAddress(to); //To Mail  
  36.   
  37. MimeMessage mimeMessage = new MimeMessage(mailSession);  
  38. mimeMessage.setFrom(fromAddress);  
  39. mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);  
  40.   
  41.   
  42. mimeMessage.setSentDate(new java.util.Date());  
  43. mimeMessage.setSubject(subject);  
  44.   
  45. // Step 3: Create a body part to hold the "text" portion of the message  
  46. System.out.println("Constructing 'text' body part");  
  47.   
  48. MimeBodyPart textBodyPart = new MimeBodyPart();  
  49. textBodyPart.setContent(messageText, "text/html;charset=gb2312");  
  50.   
  51. // Step 4: Create a body part to hold the "file" portion of the message  
  52. System.out.println("Attaching 'file' body part: " + fileName);  
  53.   
  54. MimeBodyPart fileBodyPart = new MimeBodyPart();  
  55. FileDataSource fileDataSource = new FileDataSource("E:\\a.zip");  
  56. fileBodyPart.setDataHandler(new DataHandler(fileDataSource));  
  57. fileBodyPart.setFileName(fileDataSource.getName());  
  58. //添加附件  
  59. System.out.println("Finished attaching file");  
  60.   
  61. // Step 5: Create a Multipart/container and add the parts  
  62. Multipart container = new MimeMultipart();  
  63. container.addBodyPart(textBodyPart);  
  64. container.addBodyPart(fileBodyPart);  
  65.   
  66. // Step 6: Add the Multipart to the actual message  
  67. mimeMessage.setContent(container);  
  68.   
  69. System.out.println("Message constructed");  
  70.   
  71. // Step 7: Now send the message  
  72. Transport transport = mailSession.getTransport("smtp");  
  73. transport.connect(smtpHost, "biansutao""password");  
  74. transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());  
  75. transport.close();  
  76.   
  77. System.out.println("Message sent!");  
  78. }  
  79. /* 
  80.  * 测试发送邮件 
  81.  */  
  82.   
  83. //+++++++++++++++++++++++++++++++++++++++++++++++  
  84. public static void main(String[] args) {  
  85.   
  86. String fileName = "b.zip";  
  87. String smtpHost = "smtp.163.com";  
  88. String from = "biansutao@163.com"//必须与transport.connect(smtpHost, "username1", "pwd1");的username1一样  
  89. String to = "biansutao@163.com";  
  90. String subject = "邮件测试从mogoko"//subject javamail自动转码  
  91. StringBuffer theMessage = new StringBuffer();  
  92. theMessage.append("邮件测试");  
  93.   
  94. try {  
  95. SendAttachMail.sendMessage(smtpHost, from, to, subject,  
  96. theMessage.toString(), fileName);  
  97. catch (javax.mail.MessagingException exc) {  
  98. exc.p  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值