javamail邮件发送

来源:互联网 发布:日本现代著名作家知乎 编辑:程序博客网 时间:2024/06/06 08:27

邮件发送

昨天说了在电脑上生成带附件和图片的复杂邮件,那么怎么通过javamail发送出去呢,今天就实现了。也是在原来的基础上进行完善。

需要用到的包及图片文件

下面接着在昨天的基础上添加代码:

Code:
  1. public class SendImageMail {   
  2.   
  3.   
  4. private static final String HOST = "smtp.sohu.com";   
  5.   
  6. private static final String SMTP = "smtp";   
  7.   
  8. private static final String AUTH = "true";   
  9.   
  10.   
  11. public static void main(String[] args) throws MessagingException {   
  12.   
  13. Properties props = new Properties();   
  14.   
  15. // 设置发送邮件的主机服务名称   
  16.   
  17. props.setProperty("mail.host", HOST);   
  18.   
  19. // 设置发送邮件的协议   
  20.   
  21. props.setProperty("mail.transport.protocol", SMTP);   
  22.   
  23. // 设置登录验证   
  24.   
  25. props.setProperty("mail.smtp.auth", AUTH);   
  26.   
  27. // 根据属性文件获取发送接收邮件环境对象   
  28.   
  29. Session session = Session.getDefaultInstance(props);   
  30.   
  31. // 根据环境对象创建一份邮件   
  32.   
  33. Message message = createMessage(session);   
  34.   
  35. // 根据环境对象获取发送邮件的对象   
  36.   
  37. Transport tsp = session.getTransport();   
  38.   
  39. // 调用发送邮件对象的登录连接验证的方法   
  40.   
  41. tsp.connect("redarmycsdn""redarmycsdn");   
  42.   
  43. // 调用发送邮件对象的发送邮件的方法 发送邮件   
  44.   
  45. tsp.sendMessage(message, message.getAllRecipients());   
  46.   
  47. // 关闭   
  48.   
  49. tsp.close();   
  50.   
  51.   
  52. }   
  53.   
  54.   
  55. public static Message createMessage(Session session) {   
  56.   
  57.   
  58. MimeMessage message = new MimeMessage(session);   
  59.   
  60.   
  61. try {   
  62.   
  63. // 设置邮件头   
  64.   
  65. message.setFrom(new InternetAddress("redarmycsdn@sohu.com"));   
  66.   
  67.             //发送类型及地址   
  68.   
  69. message.setRecipient(Message.RecipientType.TO, new InternetAddress(   
  70.   
  71. "redarmy_chen@qq.com"));   
  72.   
  73. message.setSubject("测试带有图片的");   
  74.   
  75.   
  76. // 设置邮件体   
  77.   
  78. MimeBodyPart part = new MimeBodyPart();   
  79.   
  80.             // 内容图片用HTML标签   
  81.   
  82. part.setContent("中国....<br/><img src='cid:xx.jpg'>",   
  83.   
  84. "text/html;charset=utf8");   
  85.   
  86.   
  87. MimeBodyPart image = new MimeBodyPart();   
  88.   
  89. image.setDataHandler(new DataHandler(new FileDataSource(   
  90.   
  91. "src//1.jpg")));   
  92.   
  93. image.setContentID("xx.jpg");   
  94.   
  95.   
  96. // 设置描述关系   
  97.   
  98. MimeMultipart mp = new MimeMultipart();   
  99.   
  100. mp.addBodyPart(part);   
  101.   
  102. mp.addBodyPart(image);   
  103.   
  104.   
  105. mp.setSubType("related");   
  106.   
  107. message.setContent(mp);   
  108.   
  109.   
  110.   
  111. //带有附件的自己完成.   
  112.   
  113.   
  114. message.saveChanges();   
  115.   
  116. catch (AddressException e) {   
  117.   
  118. // TODO Auto-generated catch block   
  119.   
  120. e.printStackTrace();   
  121.   
  122. catch (MessagingException e) {   
  123.   
  124. // TODO Auto-generated catch block   
  125.   
  126. e.printStackTrace();   
  127.   
  128. }   
  129.   
  130.   
  131. return message;   
  132.   
  133. }   
  134.   
  135.   
  136. }   
  137.   

好了邮件发送成功了,以后内容尽请期待。

原创粉丝点击