javamail的smtp方式发送邮件实例

  1. 本实例为javamail发送smtp邮件实例和常见问题解决
  2. package mail;
  3. import java.util.Date;
  4. import java.util.Properties;
  5. import javax.mail.Address;
  6. import javax.mail.Authenticator;
  7. import javax.mail.Message;
  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. * 发送普通邮件,接受普通邮件 发送带有附件的邮件,接收带有附件的邮件 发送html形式的邮件,接受html形式的邮件 发送带有图片的邮件等做了一个总结。
  15. */
  16. public class Test
  17. {
  18. // 邮箱服务器
  19. private String host = "smtp.163.com";
  20. // 这个是你的邮箱用户名 用户名一般为xxx@163.com中的xxx 但当确认无误时依然是认证错误可以修改为xxx@163.com全名测试
  21. private String username = "******";
  22. // 你的邮箱密码
  23. private String password = "******";
  24. private String mail_head_name = "this is head of this mail";
  25. private String mail_head_value = "this is head of this mail";
  26. private String mail_to = "zdw@live.cn";
  27. private String mail_from = "*****@163.com";
  28. private String mail_subject = "this is the subject of this test mail";
  29. private String mail_body = "this is the mail_body of this test mail";
  30. private String personalName = "我的邮件";
  31. public Test()
  32. {
  33. }
  34. /** *//**
  35. * 此段代码用来发送普通电子邮件
  36. */
  37. public void send() throws Exception
  38. {
  39. try
  40. {
  41. Properties props = new Properties(); // 获取系统环境
  42. Authenticator auth = new Email_Autherticator(); // 进行邮件服务器用户认证
  43. props.put("mail.smtp.host", host);
  44. props.put("mail.smtp.auth", "true");
  45. Session session = Session.getDefaultInstance(props, auth);
  46. // 设置session,和邮件服务器进行通讯。
  47. MimeMessage message = new MimeMessage(session);
  48. // message.setContent("foobar, "application/x-foobar"); // 设置邮件格式
  49. message.setSubject(mail_subject); // 设置邮件主题
  50. message.setText(mail_body); // 设置邮件正文
  51. message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题
  52. message.setSentDate(new Date()); // 设置邮件发送日期
  53. Address address = new InternetAddress(mail_from, personalName);
  54. message.setFrom(address); // 设置邮件发送者的地址
  55. Address toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址
  56. message.addRecipient(Message.RecipientType.TO, toAddress);
  57. Transport.send(message); // 发送邮件
  58. System.out.println("send ok!");
  59. } catch (Exception ex)
  60. {
  61. ex.printStackTrace();
  62. throw new Exception(ex.getMessage());
  63. }
  64. }
  65. /** *//**
  66. * 用来进行服务器对用户的认证
  67. */
  68. public class Email_Autherticator extends Authenticator
  69. {
  70. public Email_Autherticator()
  71. {
  72. super();
  73. }
  74. public Email_Autherticator(String user, String pwd)
  75. {
  76. super();
  77. username = user;
  78. password = pwd;
  79. }
  80. public PasswordAuthentication getPasswordAuthentication()
  81. {
  82. return new PasswordAuthentication(username, password);
  83. }
  84. }
  85. public static void main(String[] args)
  86. {
  87. Test sendmail = new Test();
  88. try
  89. {
  90. sendmail.send();
  91. } catch (Exception ex)
  92. {
  93. }
  94. }
  95. }

常见错误

posted on 2013-02-12 16:22  蜜雪薇琪  阅读(192)  评论(0编辑  收藏  举报