创建Java工程实现发送邮件(163邮箱)
创建Java工程
结构图
导入jar包:activation.jar/mail.jar
创建VerifyEmail类,继承Authenticator,返回账号密码验证
package com.cp.entity;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* @author chengpeng
* @date 2017年9月12日 下午4:26:01
* description 继承Authenticator,返回账号密码验证
*/
public class VerifyEmail extends Authenticator{
//成员变量
String userName = "";//账号
String password = "";//密码
//构造方法
public VerifyEmail(){
super();
}
public VerifyEmail(String userName , String password){
super();
this.userName = userName;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
创建EmailEntity,用来存放所需的数据
package com.cp.entity;
import java.io.Serializable;
/**
* @author chengpeng
* @date 2017年9月12日 下午4:38:27
* description 用来存放所需的数据
*/
public class EmailEntity implements Serializable{
private static final long serialVersionUID = 1L;
String host;//邮箱主机
Integer port;//主机端口
String userName;//发送者的账号
String password;//发送者的密码
String fromAddress;//发送者的邮箱地址
String toAddress;//接收者的邮箱地址
String subject;//设置邮件主题
String context;//设置邮件内容
String contextType;//设置邮件类型
public String getHost() {
return host;
}
public Integer getPort() {
return port;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public String getFromAddress() {
return fromAddress;
}
public String getToAddress() {
return toAddress;
}
public String getSubject() {
return subject;
}
public String getContext() {
return context;
}
public String getContextType() {
return contextType;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(Integer port) {
this.port = port;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setContext(String context) {
this.context = context;
}
public void setContextType(String contextType) {
this.contextType = contextType;
}
}
创建EmailSend,测试、发送邮件
package com.cp.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.cp.entity.EmailEntity;
import com.cp.entity.VerifyEmail;
/**
* @author chengpeng
* @date 2017年9月12日 下午5:02:16
* description 测试,发送邮件
*/
public class EmailSend {
//判断发送是否成功
public static boolean EmailSendTest(EmailEntity emailEntity){
try {
//配置文件
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", emailEntity.getHost());
properties.put("mail.smtp.port", 25);
properties.put("mail.smtp.starrttls.enable", "true");
//使用starrtls安全连接
//创建会话
VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword());
Session mailSession = Session.getInstance(properties, verifyEmail);
mailSession.setDebug(true);
//创建信息对象
Message message = new MimeMessage(mailSession);
InternetAddress from = new InternetAddress(emailEntity.getFromAddress());
InternetAddress to = new InternetAddress(emailEntity.getToAddress());
message.setFrom(from);//设置邮件信息的来源
message.setRecipient(MimeMessage.RecipientType.TO, to);//设置邮件的接收者
message.setSubject(emailEntity.getSubject());
message.setSentDate(new Date());//设置邮件发送日期
message.setContent(emailEntity.getContext() , emailEntity.getContextType());//设置邮件内容
message.saveChanges();
//发送邮件
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword());
System.out.println("trans:" + transport);
transport.sendMessage(message, message.getAllRecipients());
System.out.println("sent successful...");
return true;
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("send fial...");
return false;
}
}
//主函数测试
public static void main(String []args){
EmailEntity emailEntity = new EmailEntity();
emailEntity.setHost("smtp.163.com");
emailEntity.setPort(25);
emailEntity.setUserName("[email protected]");//有些参数自己添加
emailEntity.setPassword("......");
emailEntity.setFromAddress("[email protected]");
emailEntity.setToAddress("......");
emailEntity.setSubject("第一封邮件测试");
emailEntity.setContext("This is a emial send test...");
emailEntity.setContextType("text/html;charset=utf-8");
boolean isSend = EmailSendTest(emailEntity);
System.out.println("email send successful == " + isSend);
}
}
运行程序,测试成功