1. 普通java实现邮件发送
1.1 创建maven项目,配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < groupId> com.it</ groupId> < artifactId> emailTest</ artifactId> < version> 1.0-SNAPSHOT</ version> < properties> < maven.compiler.source> 8</ maven.compiler.source> < maven.compiler.target> 8</ maven.compiler.target> </ properties> < dependencies>
< dependency> < groupId> javax.mail</ groupId> < artifactId> javax.mail-api</ artifactId> < version> 1.6.2</ version> </ dependency> < dependency> < groupId> com.sun.mail</ groupId> < artifactId> javax.mail</ artifactId> < version> 1.6.2</ version> </ dependency>
< dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.13.2</ version> < scope> test</ scope> </ dependency> </ dependencies>
</ project>
1.2 创建邮箱码值工具类
package com. it. util ; public class EntityCode { public static final String USER = "自己的邮箱号" ; public static final String PWD = "自己的邮箱号授权码" ; 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 { 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 ) ; 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 ; } 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 ( "发送成功" ) ; }
}