前言
绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…
阿里云邮件推送控制台
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"; @Test public 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; } }
总结
到此这篇关于Java实现邮箱发送功能(阿里云邮箱推送)的文章就介绍到这了,更多相关Java邮箱发送(阿里云邮箱推送)内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。