1.导入需要的Jar 依赖文件 (仓库地址:https://mvnrepository.com)
<!-- 导入操作邮件的jar依赖 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
2. 获取邮箱的授权码(可以在相关邮箱的客户端获取), 在这里以网易邮箱作为测试, 登录网易邮箱后在设置选项中的 POP3/SMTP/IMAP 选项中找到授权密码管理,如下图
3. 编写测试代码
public class MailAuthenticator extends Authenticator {
//发件者邮箱
public static String USERNAME= "15514769010@163.com";
//邮箱授权码
public static String PASSWORD= "这里是邮箱的授权码";
public MailAuthenticator(){}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
}
public class MailOperation {
/**
*
* @param user 发件人邮箱
* @param password password授权码
* @param host host
* @param from 发件人
* @param to 接收者邮箱
* @param subject 邮件主题
* @param content 邮件内容
* @return success发送成功, failure 发送失败
* @throws Exception
*/
public String sendMail(String user, String password, String host, String from, String to, String subject, String content) throws Exception {
if (to != null){
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
MailAuthenticator.USERNAME = user;
MailAuthenticator.PASSWORD = password;
Session session = Session.getInstance(props, auth);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
if (!to.trim().equals("")){
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to.trim()));
}
message.setSubject(subject);
// 正文
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(content, "text/html;charset=utf-8");
// 整个邮件:正文+附件
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
Transport.send(message);
System.out.println(message.toString());
} catch (Exception e){
e.printStackTrace();
return "failure";
}
return "success";
}else{
return "failure";
}
}
//测试代码
public static void main(String[] args) {
MailOperation operation = new MailOperation();
String user = "15514769010@163.com";
String password = "这里是拿到的邮箱授权码";
String host = "smtp.163.com";
String from = "15514769010@163.com";
//收件人
String to = "1164300741@qq.com";
String subject = "这里是邮件的主题";
//邮箱内容
StringBuffer sb = new StringBuffer();
String yzm = RandomUtil.getRandomString(6);
sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa' style='border:1px solid #d9f4ee; font-size:14px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px; padding-bottom:5px;'><span style='font-weight:bold;'>温馨提示:</span>"
+ "<div style='width:950px;font-family:arial;'>欢迎使用*******,您的注册码为:<br/><h2 style='color:green'>"+yzm+"</h2><br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>xxx电子商务有限公司</div>"
+"</div>");
try {
String res = operation.sendMail(user, password, host, from, to,
subject, sb.toString());
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
}
}