Java实现邮箱发送功能实例(阿里云邮箱推送)
程序员文章站
2022-06-25 11:56:00
前言绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…阿里云邮件推送控制台https://www.aliyun.com/product/directmail申...
前言
绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…
阿里云邮件推送控制台
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邮箱发送(阿里云邮箱推送)内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: JS实现简单移动端鼠标拖拽