Java mail邮箱发送
- 1. 邮箱信息实体类
- 2. jar包依赖
- 3. 发送邮箱实现
绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…
- 阿里云邮件推送控制台
- https://www.aliyun.com/product/directmail
- 申请邮箱并解析域名
- https://yq.aliyun.com/articles/707997
1. 邮箱信息实体类
package cn.bitqian.mail.test;/*** 邮件内容实体类* @author echo lovely* @date 2020/9/18 15:47*/
public class MailInfo {// 发件人账户private String sendEmailAccount;// 发件人密码private String sendEmailPassword;// 收件人账户private String receiveMailAccount;// 发送人姓名private String sendPersonName;// 收件人姓名private String receivePersonName;// 邮件标题private String mailTitle;// 邮件正文private String mailContent;public MailInfo(String sendEmailAccount, String sendEmailPassword, String receiveMailAccount,String sendPersonName, String receivePersonName,String mailTitle, String mailContent) {this.sendEmailAccount = sendEmailAccount;this.sendEmailPassword = sendEmailPassword;this.receiveMailAccount = receiveMailAccount;this.sendPersonName = sendPersonName;this.receivePersonName = receivePersonName;this.mailTitle = mailTitle;this.mailContent = mailContent;}public String getSendEmailAccount() {return sendEmailAccount;}public void setSendEmailAccount(String sendEmailAccount) {this.sendEmailAccount = sendEmailAccount;}public String getSendEmailPassword() {return sendEmailPassword;}public void setSendEmailPassword(String sendEmailPassword) {this.sendEmailPassword = sendEmailPassword;}public String getReceiveMailAccount() {return receiveMailAccount;}public void setReceiveMailAccount(String receiveMailAccount) {this.receiveMailAccount = receiveMailAccount;}public String getMailTitle() {return mailTitle;}public void setMailTitle(String mailTitle) {this.mailTitle = mailTitle;}public String getMailContent() {return mailContent;}public void setMailContent(String mailContent) {this.mailContent = mailContent;}public String getSendPersonName() {return sendPersonName;}public void setSendPersonName(String sendPersonName) {this.sendPersonName = sendPersonName;}public String getReceivePersonName() {return receivePersonName;}public void setReceivePersonName(String receivePersonName) {this.receivePersonName = receivePersonName;}
}
2. jar包依赖
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- mail dependency --><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version></dependency>
3. 发送邮箱实现
注意:1. 更改下面的属性,与实体类对应
代码实现
package cn.bitqian.mail.test;import org.junit.Test;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;/*** 邮件发送工具类* @author echo lovely* @date 2020/9/18 15:54*/
public class MailSendUtils {// 邮箱smtp协议 这里是使用阿里云的public static final String myEmailSMTPHost = "smtpdm.aliyun.com";@Testpublic void test() {MailInfo mailInfo = new MailInfo("控制台所绑定的邮箱账户名","绑定账户邮箱密码","收件人邮箱名", "发件人姓名","收件人姓名", "邮箱标题", "邮箱内容");try {sendEmail(mailInfo);} catch (Exception e) {e.printStackTrace();}}public void sendEmail(MailInfo mailInfo) throws Exception {// 1. 创建参数配置, 用于连接邮件服务器的参数配置Properties props = new Properties(); // 参数配置props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址props.setProperty("mail.smtp.auth", "true"); // 需要请求认证// 2. 根据配置创建会话对象, 用于和邮件服务器交互Session session = Session.getInstance(props);session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log// 3. 创建一封邮件MimeMessage message = new MailSendUtils().createMimeMessage(session, mailInfo);// 4. 根据 Session 获取邮件传输对象Transport transport = session.getTransport();// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错transport.connect(mailInfo.getSendEmailAccount(), mailInfo.getSendEmailPassword());// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人transport.sendMessage(message, message.getAllRecipients());// 7. 关闭连接transport.close();}public MimeMessage createMimeMessage(Session session, MailInfo mailInfo) throws Exception {// 1. 创建一封邮件MimeMessage message = new MimeMessage(session);// 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)message.setFrom(new InternetAddress(mailInfo.getSendEmailAccount(), mailInfo.getSendPersonName(), "UTF-8"));// 3. To: 收件人(可以增加多个收件人、抄送、密送)message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(mailInfo.getReceiveMailAccount(), mailInfo.getReceivePersonName(), "UTF-8"));// 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)message.setSubject(mailInfo.getMailTitle(), "UTF-8");// 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)message.setContent(mailInfo.getMailContent(), "text/html;charset=UTF-8");// 6. 设置发件时间message.setSentDate(new Date());// 7. 保存设置message.saveChanges();return message;}}