需要的jar包activation-1.1.jar和mail-1.4.7.jar
bo类mm_config_email_conf.java
/**
* 邮件配置
* @author dongyazhuo
*/
public class mm_config_email_conf {
/**是否开启发送邮件*/
private boolean open;
/**发件人的邮件*/
private String email;
/**收件人 可以换成List*/
private String notice_email;
/**发件服务账号 XXX@qq.com*/
private String username;
/**发件服务授权码*/
private String password;
/**发件服务 smtp.qq.com*/
private String host;
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNotice_email() {
return notice_email;
}
public void setNotice_email(String notice_email) {
this.notice_email = notice_email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
utlis类mm_mail.java
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
/**
* 邮件推送
* @author dongyazhuo
*/
public class mm_mail {
mm_config_email_conf email_conf = null;
private String order_no;
public mm_mail(mm_config_email_conf _mm_config_email_conf) {
super();
this.email_conf=_mm_config_email_conf;
}
public void push(String order_no) {
this.order_no = order_no;
try {
send();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void send() throws Exception {
// 1、连接邮件服务器的参数配置
Properties props = new Properties();
// 设置用户的认证方式
props.setProperty("mail.smtp.auth", "true");
// 设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
// 设置发件人的SMTP服务器地址
props.setProperty("mail.smtp.host", email_conf.getHost());
// 2、创建定义整个应用程序所需的环境信息的 Session 对象
Session session = Session.getInstance(props);
// 设置调试信息在控制台打印出来
session.setDebug(true);
// 3、创建邮件的实例对象
Message msg = getMimeMessage(session);
// 4、根据session对象获取邮件传输对象Transport
Transport transport = session.getTransport();
// 设置发件人的账户名和密码
transport.connect(email_conf.getUsername(), email_conf.getPassword());
// 发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(msg, msg.getAllRecipients());
// 如果只想发送给指定的人,可以如下写法
// transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});
// 5、关闭邮件连接
transport.close();
}
public MimeMessage getMimeMessage(Session session) throws Exception {
// 创建一封邮件的实例对象
MimeMessage msg = new MimeMessage(session);
// 设置发件人地址
msg.setFrom(new InternetAddress(email_conf.getEmail()));
/**
* 设置收件人地址(可以增加多个收件人、抄送、密送),即下面这一行代码书写多行 MimeMessage.RecipientType.TO:发送 MimeMessage.RecipientType.CC:抄送
* MimeMessage.RecipientType.BCC:密送
*/
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email_conf.getNotice_email()));
// 设置邮件主题
msg.setSubject("迟到的爱情", "UTF-8");
// 设置邮件正文
msg.setContent(get_content(), "text/html;charset=UTF-8");
// 设置邮件的发送时间,默认立即发送
msg.setSentDate(new Date());
return msg;
}
private String get_content() {
return "爱不爱我";
}
}
test类mailTest.java
public class mailTest {
public static void main(String[] args) {
mm_config_email_conf thisConfig = new mm_config_email_conf();
thisConfig.setOpen(true);
thisConfig.setEmail("XXX@qq.com");
thisConfig.setHost("smtp.qq.com");
thisConfig.setNotice_email("XXX@qq.com");
thisConfig.setUsername("XXX@qq.com");
thisConfig.setPassword("XXX");
mm_mail mm_mail = new mm_mail(thisConfig);
mm_mail.push("");
}
}