package org.example; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.security.Security; import java.util.Date; import java.util.Properties; public class QiyeMailSend { public static void main(String[] args) { try{ //设置SSL连接、邮件环境 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); //协议 //props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", "smtp.aliyun.com");//smtp服务器地址 props.setProperty("mail.smtp.port", "465");//非加密端口 // 使用ssl加密方式,进行如下配置: props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtp.auth", "true");//表示SMTP发送邮件,需要进行身份验证 props.setProperty("mail.smtp.from", "XXXX");//mailfrom 参数 props.setProperty("mail.user","XXX");//发件人的账号 props.setProperty("mail.password","XXX");// 发件人的账号的密码,如果开启三方客户端安全密码请使用新生产的密码 //建立邮件会话 Session session = Session.getDefaultInstance(props, new Authenticator() { //身份认证 protected PasswordAuthentication getPasswordAuthentication() { //发件人的账号、密码 String userName = "XXX"; String password = "XXXX"; return new PasswordAuthentication(userName, password); } }); //建立邮件对象 MimeMessage message = new MimeMessage(session); //设置邮件的发件人 InternetAddress from = new InternetAddress("XXXX","客服"); //from 参数,可实现代发,注意:代发容易被收信方拒信或进入垃圾箱。 message.setFrom(from); //设置邮件的收件人 String[] to = {"XXXXXX"};//收件人列表 InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i=0;i<to.length;i++){ //System.out.println("发送到:" + to[i]); sendTo[i] = new InternetAddress(to[i]); } //传入收件人 message.setRecipients(Message.RecipientType.TO,sendTo); //设置邮件的主题 message.setSubject("邮件主题"); //设置邮件的文本 String content="<!DOCTYPE html> \n" + "<html lang=\"zh-CN\"> \n" + "<head> \n" + " <meta charset=\"UTF-8\"> \n" + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> \n" + " <title>密码找回</title> \n" + " <style> \n" + " /* 简单的内联样式 */ \n" + " body { \n" + " font-family: Arial, sans-serif; \n" + " background-color: #f4f4f4; \n" + " margin: 0; \n" + " padding: 0; \n" + " } \n" + " .container { \n" + " max-width: 600px; \n" + " margin: 0 auto; \n" + " background-color: #fff; \n" + " padding: 40px; \n" + " box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); \n" + " border-radius: 8px; \n" + " margin-top: 50px; \n" + " } \n" + " h1 { \n" + " margin-top: 0; \n" + " } \n" + " p { \n" + " line-height: 1.6; \n" + " color: #333; \n" + " } \n" + " a { \n" + " color: #007bff; \n" + " text-decoration: none; \n" + " } \n" + " a:hover { \n" + " text-decoration: underline; \n" + " } \n" + " </style> \n" + "</head> \n" + "<body> \n" + " <div class=\"container\"> \n" + " <h1>密码找回请求</h1> \n" + " <p>您好,</p> \n" + " <p>我们收到了一个关于重置您在[您的网站名称]账户密码的请求。</p> \n" + " <p>如果您本人发起了此请求,请点击下面的链接来重置您的密码:</p> \n" + " <p> \n" + " <a href=\"https://[您的网站].com/reset-password?token=[随机生成的令牌]\">重置密码</a> \n" + " </p> \n" + " <p>此链接将在[链接有效期,如24小时]后失效。</p> \n" + " <p>如果您没有请求重置密码,请忽略此邮件。我们建议您检查您的账户安全设置,并确保您的密码强且唯一。</p> \n" + " <p>如果您需要进一步帮助或有任何疑问,请随时回复此邮件或访问我们的<a href=\"https://[您的网站].com/support\">客服支持页面</a>。</p> \n" + " <p>感谢您的使用和支持!</p> \n" + " <p>祝好,</p> \n" + " <p>[您的网站名称] 团队</p> \n" + " </div> \n" + "</body> \n" + "</html>"; message.setContent(content,"text/html;charset=UTF-8"); //设置时间 message.setSentDate(new Date()); message.saveChanges(); //发送邮件 Transport.send(message); System.out.println("发送成功!"); }catch(Exception e){ System.out.println("异常:"+e.toString()); } } }
密码找回阿里云
最新推荐文章于 2024-08-26 16:40:46 发布