public class EmailUtil {
/**
* todo 发送QQ邮箱
*/
public void sendQQEmail(String emailAddress){
//做链接前的准备工作 也就是参数初始化
Properties properties = new Properties();
properties.setProperty("mail.smtp.host","smtp.qq.com");//发送邮箱服务器
properties.setProperty("mail.smtp.port","465");//发送端口
properties.setProperty("mail.smtp.auth","true");//是否开启权限控制
properties.setProperty("mail.debug","true");//true 打印信息到控制台
properties.setProperty("mail.transport","smtp");//发送的协议是简单的邮件传输协议
properties.setProperty("mail.smtp.ssl.enable","true");
//建立两点之间的链接
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("675361896@qq.com","wbomvmxtifjybcbb");
}
});
//创建邮件对象
Message message = new MimeMessage(session);
//设置发件人
try {
message.setFrom(new InternetAddress("675361896@qq.com"));
//设置收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress(emailAddress));//收件人
//设置主题
message.setSubject("Activiti工作流审核结果通知");
//设置邮件正文 第二个参数是邮件发送的类型
message.setContent("尊敬的用户您好,您的请假申请已通过,请知悉!","text/html;charset=UTF-8");
//发送一封邮件
Transport transport = session.getTransport();
transport.connect("675361896@qq.com","wbomvmxtifjybcbb");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* todo 发送网易163邮箱
*/
// 发件人 账号和密码
public static final String MY_EMAIL_ACCOUNT = "fantongxue666@163.com";
public static final String MY_EMAIL_PASSWORD = "PCRAVAFQNZVIJDOI";// 密码,是你自己的设置的授权码
// SMTP服务器(这里用的163 SMTP服务器)
public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
public static final String SMTP_163_PORT = "25";// 端口号,这个是163使用到的;QQ的应该是465或者875
// 收件人
public static final String RECEIVE_EMAIL_ACCOUNT = "fantongxue666@163.com";
@Test
public void sendWY163Email(String emailAddress) throws Exception{
Properties p = new Properties();
p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
p.setProperty("mail.smtp.port", SMTP_163_PORT);
p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");
Session session = Session.getInstance(p, new Authenticator() {
// 设置认证账户信息
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
}
});
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
// 发件人
message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
// 收件人和抄送人
message.setRecipients(Message.RecipientType.TO, RECEIVE_EMAIL_ACCOUNT);
// message.setRecipients(Message.RecipientType.CC, MY_EMAIL_ACCOUNT);
// 内容(这个内容还不能乱写,有可能会被SMTP拒绝掉;多试几次吧)
message.setSubject("Activiti工作流审核结果通知");
message.setContent("尊敬的用户您好,您的请假申请已通过,请知悉!", "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
Transport.send(message);
}
}
qq发邮件
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class EmailUtil {
/**
* todo 发送QQ邮箱
*/
public static void sendQQEmail(String account ,String passWord ,String adresss) throws GeneralSecurityException {
// 收件人电子邮箱
String to = adresss;
// 发件人电子邮箱xx
String from = "6x'x97824@qq.com";
// 指定发送邮件的主机为 smtp.qq.com
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("63x'x24@qq.com", "utgx'xgupx'xcea");
}
});xx
try {xxxx
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("x'xd科技有限xx公司:账户信息");
// 设置消息体
message.setText("您的账号为:"+ account +"密码 :" + passWord );
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}