我觉得学会Java mail是一件很自豪的事,怎么说呢,邮箱这么有逼格的东西都能被你玩的很溜的话,一定不一般。
本次试验使用了javax.mail.jar
jar包,请自行百度下载。
我实现的Java mail主要包括4个部分:
- 发送邮件使用的基本信息
- 邮件发送器
- 发件人设置
- 实际发送
四个部分组成。
发送邮件使用的基本信息
文件名:MailSenderInfo.java
代码如下,我仍然以备注的形式讲解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
package net.kuangmeng.mail;
import java.util.Properties; public class MailSenderInfo { private String mailServerHost; private String mailServerPort = "25"; private String fromAddress; private String toAddress; private String userName; private String password; private boolean validate = false; private String subject; private String content; private String[] attachFileNames;
public Properties getProperties(){ Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", validate ? "true" : "false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } }
|
邮件发送器
文件名:SimpleMailSender.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
package net.kuangmeng.mail; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart;
public class SimpleMailSender {
public boolean sendTextMail(MailSenderInfo mailInfo) { MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; }
public static boolean sendHtmlMail(MailSenderInfo mailInfo){ MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } }
|
发件人设置
文件名:MyAuthenticator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
package net.kuangmeng.mail; import javax.mail.*;
public class MyAuthenticator extends Authenticator{ String userName=null; String password=null; public MyAuthenticator(){ } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } }
|
实际发送
文件名:MailAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
package net.kuangmeng.mail; import net.kuangmeng.*; public class MailAction { @SuppressWarnings("static-access") public static void main(String[] args){ MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.yeah.net"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("*****@yeah.net"); mailInfo.setPassword("******"); mailInfo.setFromAddress("*****@yeah.net"); mailInfo.setToAddress("****@qq.com"); mailInfo.setSubject("你好!"); mailInfo.setContent("这是一个测试"); SimpleMailSender sms = new SimpleMailSender(); sms.sendTextMail(mailInfo); sms.sendHtmlMail(mailInfo); } }
|
The link of this page is https://blog.nooa.tech/articles/21f3b9c2/ . Welcome to reproduce it!