javamail发送文本邮件、HTML邮件及带附件邮件
- 格式:docx
- 大小:23.86 KB
- 文档页数:12
javamail发送文本邮件、HTML邮件及带附件邮件
MailInfo.java
[java] view plain copy
1. import java.util.Properties;
2.
3. public class MailInfo {
4. private String mailServerHost;// 服务器ip
5. private String mailServerPort;// 端口
6. private String fromAddress;// 发送者的邮件地址
7. private String toAddress;// 邮件接收者地址
8. private String username;// 登录邮件发送服务器的用户名
9. private String password;// 登录邮件发送服务器的密码
10. private boolean validate = false;// 是否需要身份验证
11. private String subject;// 邮件主题
12. private String content;// 邮件内容
13. private String[] attachFileNames;// 附件名称
14.
15. public Properties getProperties() {
16. Properties p = new Properties();
17. p.put("mail.smtp.host", this.mailServerHost);
18. p.put("mail.smtp.port", this.mailServerPort);
19. p.put("mail.smtp.auth", validate ? "true" : "false");
20. return p;
21. }
22.
23. public String getMailServerHost() {
24. return mailServerHost;
25. }
26. 27. public void setMailServerHost(String mailServerHost)
{
28. this.mailServerHost = mailServerHost;
29. }
30.
31. public String getMailServerPort() {
32. return mailServerPort;
33. }
34.
35. public void setMailServerPort(String mailServerPort) {
36. this.mailServerPort = mailServerPort;
37. }
38.
39. public String getFromAddress() {
40. return fromAddress;
41. }
42.
43. public void setFromAddress(String fromAddress) {
44. this.fromAddress = fromAddress;
45. }
46.
47. public String getToAddress() {
48. return toAddress;
49. }
50.
51. public void setToAddress(String toAddress) {
52. this.toAddress = toAddress;
53. }
54. 55. public String getUsername() {
56. return username;
57. }
58.
59. public void setUsername(String username) {
60. ername = username;
61. }
62.
63. public String getPassword() {
64. return password;
65. }
66.
67. public void setPassword(String password) {
68. this.password = password;
69. }
70.
71. public boolean isValidate() {
72. return validate;
73. }
74.
75. public void setValidate(boolean validate) {
76. this.validate = validate;
77. }
78.
79. public String getSubject() {
80. return subject;
81. }
82.
83. public void setSubject(String subject) {
84. this.subject = subject; 85. }
86.
87. public String getContent() {
88. return content;
89. }
90.
91. public void setContent(String content) {
92. this.content = content;
93. }
94.
95. public String[] getAttachFileNames() {
96. return attachFileNames;
97. }
98.
99. public void setAttachFileNames(String[] attachFileNames) {
100. this.attachFileNames = attachFileNames;
101. }
102.
103. }
MyAuthenticator.Java
[java] view plain copy
1. import javax.mail.Authenticator;
2. import javax.mail.PasswordAuthentication;
3.
4. public class MyAuthenticator extends Authenticator {
5. private String username = null;
6. private String password = null;
7. 8. public MyAuthenticator() {
9. };
10.
11. public MyAuthenticator(String username, String password) {
12. ername = username;
13. this.password = password;
14. }
15.
16. protected PasswordAuthentication getPasswordAuthentication() {
17. return new PasswordAuthentication(username, password);
18. }
19.
20. }
SimpleMail.java
[java] view plain copy
1. import java.io.File;
2. import java.util.Date;
3. import java.util.Properties;
4.
5. import javax.activation.DataHandler;
6. import javax.activation.FileDataSource;
7. import javax.mail.Address;
8. import javax.mail.Message;
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. public class SimpleMail {
18.
19. // 以文本格式发送邮件
20. public static boolean sendTextMail(MailInfo mailInfo) {
21. //判断是否需要身份认证
22. MyAuthenticator authenticator = null;
23. Properties properties = mailInfo.getProperties();
24. if (mailInfo.isValidate()) {
25. //如果需要身份认证,则创建一个密码验证器
26. authenticator = new MyAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());
27. }
28.
29. //根据邮件会话属性和密码验证器构造一个发送邮件的session
30. Session sendMailSession = Session.getDefaultInstance(properties, authenticator);
31. try {
32. Message mailMessage = new MimeMessage(sendMailSession);//根据session创建一个邮件消息
33. Address from = new InternetAddress(mailInfo.getFromAddress());//创建邮件发送者地址
34. mailMessage.setFrom(from);//设置邮件消息的发送者
35. Address to = new InternetAddress(mailInfo.getToAddr