java发送邮件工具类
创始人
2024-12-03 11:03:44
0

1. 普通java实现邮件发送

在这里插入图片描述

1.1 创建maven项目,配置pom.xml文件

      4.0.0      com.it     emailTest     1.0-SNAPSHOT               8         8                                 javax.mail             javax.mail-api             1.6.2                               com.sun.mail             javax.mail             1.6.2                                junit             junit             4.13.2             test                

在这里插入图片描述

1.2 创建邮箱码值工具类

package com.it.util;  public class EntityCode {     //发件人邮箱     public static final String USER = "自己的邮箱号";     //授权码     public static final String PWD = "自己的邮箱号授权码";     //发送给对应的app,如果是163邮箱:smtp.163.com     public static final String HOST = "smtp.qq.com"; } 

1.3 创建邮箱发送邮件的工具类

package com.it.util;  import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties;  public class MailUtil {     /**      * 创建邮件消息      * @return 创建的邮件消息      */     private static MimeMessage createMail(){         try {             Properties properties = new Properties();             properties.put("mail.smtp.auth",true);             properties.put("mail.smtp.host", EntityCode.HOST);             properties.put("mail.user",EntityCode.USER);             properties.put("mail.password",EntityCode.PWD);             // 构建授权信息,用于进行SMTP进行身份验证             Authenticator authenticator = new Authenticator() {                 @Override                 protected PasswordAuthentication getPasswordAuthentication() {                     return new PasswordAuthentication(EntityCode.USER, EntityCode.PWD);                 }             };             // 使用环境属性和授权信息,创建邮件会话             Session mailSession = Session.getInstance(properties, authenticator);             // 创建邮件消息             MimeMessage message = new MimeMessage(mailSession);             return message;         }catch (Exception e){             e.printStackTrace();         }         return null;     }      /**      * 发送一个简单的文本邮件      * @param to 收件人邮箱      * @param title 邮件标题      * @param text  邮件内容      * @return      */     public static boolean sendMail(String to,String title,String text){         MimeMessage message = createMail();         if (message==null){             return false;         }         try {             // 设置发件人             InternetAddress form = new InternetAddress(EntityCode.USER);             message.setFrom(form);              // 设置收件人             InternetAddress toAddress = new InternetAddress(to);             message.setRecipient(Message.RecipientType.TO, toAddress);              // 设置邮件标题             message.setSubject(title);              // 设置邮件的内容体             message.setContent(text, "text/html;charset=UTF-8");             // 发送邮件             Transport.send(message);         }catch (Exception e){             e.printStackTrace();         }         return true;     } } 

1.4 创建测试类

import com.it.util.MailUtil; import com.it.util.UUIDUtil; import org.junit.Test;  public class EmailTest {      @Test     public void Test1(){         String email = "2428596932@qq.com";         String title = "测试邮件";         String text  = UUIDUtil.getUUID();          boolean b = MailUtil.sendMail(email, title, text);         System.out.println("发送成功");     } } 

相关内容

热门资讯

脚本解密!wpk透视辅助方法,... 脚本解密!wpk透视辅助方法,有哪些免费的wpk作必弊码(透视)真是是真的app(哔哩哔哩);1、上...
脚本解谜!hhpoker软件安... 脚本解谜!hhpoker软件安装包,hhpoker视频巡查真的假的(透视)一直真的是有插件(哔哩哔哩...
脚本分享!wpk显示有作必弊,... 脚本分享!wpk显示有作必弊,wpk俱乐部辅助器(透视)总是真的有软件(哔哩哔哩)1、让任何用户在无...
透视关于!wepoker怎么拿... 透视关于!wepoker怎么拿到好牌,wepokerplus辅助(透视)确实真的是有app(哔哩哔哩...
透视详情!hhpoker怎么破... 透视详情!hhpoker怎么破解,hhpoker底牌透视脚本(透视)总是真的有技巧(哔哩哔哩)1、实...
脚本透视!aapoker脚本,... 脚本透视!aapoker脚本,aapoker如何设置胜率(透视)总是是有技巧(哔哩哔哩)小薇(辅助器...
脚本详情!德普之星辅助工具如何... 脚本详情!德普之星辅助工具如何设置,德普之星辅助工具如何打开(透视)总是真的是有脚本(哔哩哔哩)脚本...
透视推荐!wpk官网下载链接,... 透视推荐!wpk官网下载链接,wpk透视辅助(透视)都是是真的教程(哔哩哔哩)1、超多福利:超高返利...
脚本详情!wpk辅助器,wpk... 脚本详情!wpk辅助器,wpk德州局怎么透视(透视)好像是真的软件(哔哩哔哩)1、德州局怎么透视破解...
透视分享!wejoker透视方... 透视分享!wejoker透视方法,wepoker黑侠辅助器(透视)都是真的有脚本(哔哩哔哩)1、用户...