1.添加Maven依賴
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
2.創建email.properties,設置發郵件郵箱的用戶名密碼
email.username=
email.password=
3.添加EmailAuthenticator繼承Authenticator
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmailAuthenticator extends Authenticator {
private String username;
private String password;
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
4.創建EmailUtil工具類
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public abstract class EmailUtil {
private static Session session = null;
private static EmailAuthenticator authenticator = null;
static {
try {
InputStream inputStream = EmailUtil.class.getResourceAsStream("/email.properties");
Properties properties = new Properties();
properties.load(inputStream);
authenticator = new EmailAuthenticator();
String username = properties.getProperty("email.username");
authenticator.setUsername(username);
String password = properties.getProperty("email.password");
authenticator.setPassword(password);
String smtpHostName = "smtp." + username.split("@")[1];
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", smtpHostName);
session = Session.getInstance(properties, authenticator);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private EmailUtil() { }
/**
* 通用發郵件方法
*/
private static void send(List<String> recipients, SimpleEmail email) throws MessagingException {
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(authenticator.getUsername()));
InternetAddress[] addresses = new InternetAddress[recipients.size()];
for (int index = 0; index < recipients.size(); index ++) {
addresses[index] = new InternetAddress(recipients.get(index));
}
message.setRecipients(RecipientType.TO, addresses);
message.setSubject(email.getSubject());
message.setContent(email.getContent(), "text/html;charset=utf-8");
Transport.send(message);
}
/**
* 發送郵件
*/
public static void send(String recipient, SimpleEmail email) throws MessagingException {
List<String> recipients = new ArrayList<String>();
recipients.add(recipient);
send(recipients, email);
}
/**
* 群發郵件
*/
public static void massSend(List<String> recipients, SimpleEmail email) throws MessagingException {
send(recipients, email);
}
}
5.測試
public static void main(String[] args) throws Exception {
SimpleEmail simpleEmail = new SimpleEmail();
simpleEmail.setSubject("今天你學習了么?");
simpleEmail.setContent("今天你寫博客了么");
List<String> recipients = new ArrayList<String>();
recipients.add("2668659188@qq.com");
massSend(recipients, simpleEmail);
}
代碼地址:https://gitee.com/jsjack_wang/JavaDemo
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。