文件名称:Java邮件发送
文件大小:67KB
文件格式:DOC
更新时间:2018-01-14 03:27:26
邮件
用于Java的邮件发送的一个类方法 Java邮件发送类 这是第一部分第一个类:MailSenderInfo.java 1. package com.util.mail; 2. /** 3. * 发送邮件需要使用的基本信息 4. */ 5. import java.util.Properties; 6. public class MailSenderInfo { 7. // 发送邮件的服务器的IP和端口 8. private String mailServerHost; 9. private String mailServerPort = "25"; 10. // 邮件发送者的地址 11. private String fromAddress; 12. // 邮件接收者的地址 13. private String toAddress; 14. // 登陆邮件发送服务器的用户名和密码 15. private String userName; 16. private String password; 17. // 是否需要身份验证 18. private boolean validate = false; 19. // 邮件主题 20. private String subject; 21. // 邮件的文本内容 22. private String content; 23. // 邮件附件的文件名 24. private String[] attachFileNames; 25. /** 26. * 获得邮件会话属性 27. */ 28. public Properties getProperties(){ 29. Properties p = new Properties(); 30. p.put("mail.smtp.host", this.mailServerHost); 31. p.put("mail.smtp.port", this.mailServerPort); 32. p.put("mail.smtp.auth", validate ? "true" : "false"); 33. return p; 34. } 35. public String getMailServerHost() { 36. return mailServerHost; 37. } 38. public void setMailServerHost(String mailServerHost) { 39. this.mailServerHost = mailServerHost; 40. } 41. public String getMailServerPort() { 42. return mailServerPort; 43. } 44. public void setMailServerPort(String mailServerPort) { 45. this.mailServerPort = mailServerPort; 46. } 47. public boolean isValidate() { 48. return validate; 49. } 50. public void setValidate(boolean validate) { 51. this.validate = validate; 52. } 53. public String[] getAttachFileNames() { 54. return attachFileNames; 55. } 56. public void setAttachFileNames(String[] fileNames) { 57. this.attachFileNames = fileNames; 58. } 59. public String getFromAddress() { 60. return fromAddress; 61. } 62. public void setFromAddress(String fromAddress) { 63. this.fromAddress = fromAddress; 64. } 65. public String getPassword() { 66. return password; 67. } 68. public void setPassword(String password) { 69. this.password = password; 70. } 71. public String getToAddress() { 72. return toAddress; 73. } 74. public void setToAddress(String toAddress) { 75. this.toAddress = toAddress; 76. } 77. public String getUserName() { 78. return userName; 79. } 80. public void setUserName(String userName) { 81. this.userName = userName; 82. } 83. public String getSubject() { 84. return subject; 85. } 86. public void setSubject(String subject) { 87. this.subject = subject; 88. } 89. public String getContent() { 90. return content; 91. } 92. public void setContent(String textContent) { 93. this.content = textContent; 94. } 95. } 第二部分第二个类:SimpleMailSender.java Java代码 1. package com.util.mail; 2. 3. import java.util.Date; 4. import java.util.Properties; 5. import javax.mail.Address; 6. import javax.mail.BodyPart; 7. import javax.mail.Message; 8. import javax.mail.MessagingException; 9. import javax.mail.Multipart; 10. import javax.mail.Session; 11. import javax.mail.Transport; 12. import javax.mail.internet.InternetAddress; 13. import javax.mail.internet.MimeBodyPart; 14. import javax.mail.internet.MimeMessage; 15. import javax.mail.internet.MimeMultipart; 16. 17. /** 18. * 简单邮件(不带附件的邮件)发送器 19. */ 20. public class SimpleMailSender { 21. /** 22. * 以文本格式发送邮件 23. * @param mailInfo 待发送的邮件的信息 24. */ 25. public boolean sendTextMail(MailSenderInfo mailInfo) { 26. // 判断是否需要身份认证 27. MyAuthenticator authenticator = null; 28. Properties pro = mailInfo.getProperties(); 29. if (mailInfo.isValidate()) { 30. // 如果需要身份认证,则创建一个密码验证器 31. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 32. } 33. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 34. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 35. try { 36. // 根据session创建一个邮件消息 37. Message mailMessage = new MimeMessage(sendMailSession); 38. // 创建邮件发送者地址 39. Address from = new InternetAddress(mailInfo.getFromAddress()); 40. // 设置邮件消息的发送者 41. mailMessage.setFrom(from); 42. // 创建邮件的接收者地址,并设置到邮件消息中 43. Address to = new InternetAddress(mailInfo.getToAddress()); 44. mailMessage.setRecipient(Message.RecipientType.TO,to); 45. // 设置邮件消息的主题 46. mailMessage.setSubject(mailInfo.getSubject()); 47. // 设置邮件消息发送的时间 48. mailMessage.setSentDate(new Date()); 49. // 设置邮件消息的主要内容 50. String mailContent = mailInfo.getContent(); 51. mailMessage.setText(mailContent); 52. // 发送邮件 53. Transport.send(mailMessage); 54. return true; 55. } catch (MessagingException ex) { 56. ex.printStackTrace(); 57. } 58. return false; 59. } 60. 61. /** 62. * 以HTML格式发送邮件 63. * @param mailInfo 待发送的邮件信息 64. */ 65. public static boolean sendHtmlMail(MailSenderInfo mailInfo){ 66. // 判断是否需要身份认证 67. MyAuthenticator authenticator = null; 68. Properties pro = mailInfo.getProperties(); 69. //如果需要身份认证,则创建一个密码验证器 70. if (mailInfo.isValidate()) { 71. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 72. } 73. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 74. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 75. try { 76. // 根据session创建一个邮件消息 77. Message mailMessage = new MimeMessage(sendMailSession); 78. // 创建邮件发送者地址 79. Address from = new InternetAddress(mailInfo.getFromAddress()); 80. // 设置邮件消息的发送者 81. mailMessage.setFrom(from); 82. // 创建邮件的接收者地址,并设置到邮件消息中 83. Address to = new InternetAddress(mailInfo.getToAddress()); 84. // Message.RecipientType.TO属性表示接收者的类型为TO 85. mailMessage.setRecipient(Message.RecipientType.TO,to); 86. // 设置邮件消息的主题 87. mailMessage.setSubject(mailInfo.getSubject()); 88. // 设置邮件消息发送的时间 89. mailMessage.setSentDate(new Date()); 90. // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 91. Multipart mainPart = new MimeMultipart(); 92. // 创建一个包含HTML内容的MimeBodyPart 93. BodyPart html = new MimeBodyPart(); 94. // 设置HTML内容 95. html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 96. mainPart.addBodyPart(html); 97. // 将MiniMultipart对象设置为邮件内容 98. mailMessage.setContent(mainPart); 99. // 发送邮件 100. Transport.send(mailMessage); 101. return true; 102. } catch (MessagingException ex) { 103. ex.printStackTrace(); 104. } 105. return false; 106. } 107. } 第三个类:MyAuthenticator.java Java代码 1. package com.util.mail; 2. 3. import javax.mail.*; 4. 5. public class MyAuthenticator extends Authenticator{ 6. String userName=null; 7. String password=null; 8. 9. public MyAuthenticator(){ 10. } 11. public MyAuthenticator(String username, String password) { 12. this.userName = username; 13. this.password = password; 14. } 15. protected PasswordAuthentication getPasswordAuthentication(){ 16. return new PasswordAuthentication(userName, password); 17. } 18. } 19. 下面给出使用上面三个类的代码: Java代码 1. public static void main(String[] args){ 2. //这个类主要是设置邮件 3. MailSenderInfo mailInfo = new MailSenderInfo(); 4. mailInfo.setMailServerHost("smtp.163.com"); 5. mailInfo.setMailServerPort("25"); 6. mailInfo.setValidate(true); 7. mailInfo.setUserName("han2000lei@163.com"); 8. mailInfo.setPassword("**********");//您的邮箱密码 9. mailInfo.setFromAddress("han2000lei@163.com"); 10. mailInfo.setToAddress("han2000lei@163.com"); 11. mailInfo.setSubject("设置邮箱标题"); 12. mailInfo.setContent("设置邮箱内容"); 13. //这个类主要来发送邮件 14. SimpleMailSender sms = new SimpleMailSender(); 15. sms.sendTextMail(mailInfo);//发送文体格式 16. sms.sendHtmlMail(mailInfo);//发送html格式 17. }