java实现邮箱发送邮件
第一步:封装发件人账号密码
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 发件人账号密码
* @author zhangdi
*
*/
public class MailAuthenticator extends Authenticator{
public static String USERNAME = "";
public static String PASSWORD = "";
public MailAuthenticator() {
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
}
第二步:提供发送操作类
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;/**
* 邮件发送操作类
*
* @author zhangdi
*
*/
public class MailOperation {
/**
* 发送邮件
* @param user 发件人邮箱
* @param password 授权码(注意不是邮箱登录密码)
* @param 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);
// mp.addBodyPart(mbp2);
message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
trans.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 = "你的邮箱地址"; String password = "你的邮箱授权码"; String host = "smtp.163.com"; String from = "你的邮箱地址"; String to = "目标邮箱地址";// 收件人 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;'>欢迎使用NET微活动,您的注册码为:<br/><h2 style='color:green'>"+yzm+"</h2><br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>杭州恩意替电子商务有限公司</div>" +"</div>"); try { String res = operation.sendMail(user, password, host, from, to, subject, sb.toString()); System.out.println(res); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Gitee码云:https://gitee.com/lyc96/projects