2010年7月21日星期三

[转载]JavaMail 发送邮件

做了一个小小的程序, 来进行通过 163.com 发送邮件的工作:

Java代码
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. /**
  6. *
  7. * 功能描述:发送邮件
  8. * 开发人员:maxl
  9. * 开发时间:2010-6-16上午03:12:16
  10. */
  11. public class MailSender {
  12. /** 发信人 */
  13. private String from;
  14. /** 收信人 */
  15. private String to;
  16. /** 主题 */
  17. private String subject;
  18. /** 正文 */
  19. private String body;
  20. private static Properties props = new Properties();
  21. static {
  22. try {
  23. InputStream in = MailSender.class
  24. .getResourceAsStream("MailSender.ini");
  25. props.load(in);
  26. in.close();
  27. } catch (Exception ex) {
  28. System.err.println("无法加载配置文件 MailSender.ini:" + ex.getMessage());
  29. ex.printStackTrace();
  30. }
  31. }
  32. public MailSender() {
  33. }
  34. /**
  35. * 发送邮件.
  36. * @return boolean - 发送结果
  37. */
  38. public boolean sendMail() {
  39. if (getBody() == null || getTo() == null || getFrom() == null
  40. || getSubject() == null) { return false; }
  41. //--[ Obtain a session
  42. try {
  43. //--[ Set up the default parameters
  44. // Properties props = new Properties();
  45. // props.put("mail.transport.protocol", "smtp" );
  46. // props.put("mail.smtp.host", smtpServer );
  47. // props.put("mail.smtp.port", "25" );
  48. //--[ Create the session and create a new mail message
  49. Session mailSession = Session.getDefaultInstance(props);
  50. Message msg = new MimeMessage(mailSession);
  51. //--[ Set the FROM, TO, DATE and SUBJECT fields
  52. msg.setFrom(new InternetAddress(getFrom()));
  53. msg.addRecipients(Message.RecipientType.TO, InternetAddress
  54. .parse(getTo()));
  55. msg.setSentDate(new Date());
  56. msg.setSubject(getSubject());
  57. //--[ Create the body of the mail
  58. msg.setText(getBody());
  59. msg.saveChanges();
  60. // Using the mail authentication, transport.connect(host, user, password)
  61. Transport transport = mailSession.getTransport("smtp");
  62. transport.connect(props.getProperty("mail.smtp.host"), props
  63. .getProperty("username"), props.getProperty("password"));
  64. transport.sendMessage(msg, msg.getAllRecipients());
  65. transport.close();
  66. // System.out.println("The email below was sent successfully");
  67. } catch (Exception e) {
  68. System.out.println(e);
  69. // e.printStackTrace();
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * @return Returns the body.
  76. */
  77. public String getBody() {
  78. return body;
  79. }
  80. /**
  81. * @param body
  82. * The body to set.
  83. */
  84. public void setBody(String body) {
  85. this.body = body;
  86. }
  87. /**
  88. * @return Returns the from.
  89. */
  90. public String getFrom() {
  91. return from;
  92. }
  93. /**
  94. * @param from
  95. * The from to set.
  96. */
  97. public void setFrom(String from) {
  98. this.from = from;
  99. }
  100. /**
  101. * @return Returns the subject.
  102. */
  103. public String getSubject() {
  104. return subject;
  105. }
  106. /**
  107. * @param subject
  108. * The subject to set.
  109. */
  110. public void setSubject(String subject) {
  111. this.subject = subject;
  112. }
  113. /**
  114. * @return Returns the to.
  115. */
  116. public String getTo() {
  117. return to;
  118. }
  119. /**
  120. * @param to
  121. * The to to set.
  122. */
  123. public void setTo(String to) {
  124. this.to = to;
  125. }
  126. public static void main(String[] args) {
  127. MailSender sender = new MailSender();
  128. sender.setFrom("admin@mxl.name");
  129. sender.setTo("130105521@qq.com");
  130. sender.setSubject("测试邮件-temp");
  131. sender.setBody("测试邮件发送成功!");
  132. System.out.println(sender.sendMail());
  133. }
  134. }

配置文件名称为 MailSender.ini, 和类文件放在同一目录下(同一包里), 内容如下:

Java代码
  1. # SMTP 主机地址
  2. mail.smtp.host = smtp.163.com
  3. # SMTP 帐号用户名
  4. username = mailuser
  5. # SMTP 帐号密码
  6. password = mailpassword
  7. mail.transport.protocol = smtp
  8. mail.smtp.port = 25
  9. mail.smtp.auth = true

最后一行指定 SMTP 需要验证.

修改这个配置文件为实际的帐号, 然后运行 java MailSender, 即可发送邮件.

注:需要mail.jar 和 activation.jar 支持!

本程序已经过QQ、163、新浪、googleMail测试成功!


没有评论:

发表评论