package com.suyuan.service.employee;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
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 test03 {
public static void sendMail() {
/*
* 1.文本内容
*/
StringBuffer buffer = new StringBuffer();
buffer.append("姓名:小明").append("\n");
buffer.append("年龄:18").append("\n");
buffer.append("爱好:打篮球").append("\n");
buffer.append(" ---来自官网").append("\n");
/*
* * 2.配置协议 *
*/
Properties props = new Properties();
// 协议
props.setProperty("mail.transport.protocol", "smtps");
// 服务器
props.setProperty("mail.smtp.host", "smtp.163.com");
// 端口
props.setProperty("mail.smtp.port", "25");
// 使用smtp身份验证
props.setProperty("mail.smtp.auth", "true");
// 使用SSL,企业邮箱必需!
// 开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
// 发送邮箱账号密码
Session mailSession = Session.getDefaultInstance(props, new MyAuthenricator("发送者邮箱账号", "发送者邮箱密码"));
/*
* * 3.配置邮件会话之后,要编写消息
* *要编写消息就要生成javax.mail.Message子类的实例或对Internet邮件使用
* javax.mail.interet.MimeMessage类。 *
*/
MimeMessage mimeMessage = new MimeMessage(mailSession);
try {
// 消息发送者、日期、主题
mimeMessage.setFrom(new InternetAddress("发送者邮箱账号"));
mimeMessage.setSentDate(new Date());
mimeMessage.setSubject("java邮箱测试");
// 设置消息的接受者与发送者(寻址接收)
mimeMessage.addRecipient(RecipientType.TO, new InternetAddress("接受者邮箱账号"));
// 设置消息内容
mimeMessage.setText(buffer.toString());
/*
* * 4.发送Email,这里以文本消息为例 *
*/
// 保存发送信息
mimeMessage.saveChanges();
// 定义发送协议
Transport transport = mailSession.getTransport("smtp");
// 登录邮箱
// transport.connect("账号","密码");
// 发送邮件
transport.send(mimeMessage);
System.out.println("消息发送成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 在真正使用创建的过程中,往往会让我们验证密码,这是我们要写一个密码验证类。javax.mail.Authenticator是一个抽象类,
* 我们要写MyAuthenticator的密码验证类,该类继承Authenticator实现:
*/
// 用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
static class MyAuthenricator extends Authenticator {
String u = null;
String p = null;
public MyAuthenricator(String u, String p) {
this.u = u;
this.p = p;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(u, p);
}
}
}
这里注意一下
//用 "smtp.exmail.qq.com" 不好使的话可以换成下面的
props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
一般免费的邮箱,如下列出的,他们的SMTP服务器就是在域名前加上smtp就行了.
smtp.163.com //端口号 :25 用163邮箱作为发送者测试
smtp.21cn.com
smtp.sina.com.cn
smtp.sohu.com
smtp.126.com
smtp.qq.com //端口号 :465或587 用qq邮箱作为发送者测试
//使用哪家的邮箱作为发送者 就要用哪家的服务地址
这个地方要注意 这两个账号要一致!
设置附件:
MimeMultipart multipart = new MimeMultipart();
File file = new File
MimeBodyPart affixPart = new MimeBodyPart();
affixPart.setDataHandler(new DataHandler(new FileDataSource(file)));// 读取附件
affixPart.setFileName(MimeUtility.encodeText(file.getName()));//设置附件标题
multipart.addBodyPart(affixPart,个数);// 设置附件
上面是 非SSL发送,如果用 SSL 发送 加如下配置
//配置ssl访问
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 只处理SSL的连接,对于非SSL的连接不做处理
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "SSL的端口号");
//注意 这个也要改为ssl端口
props.setProperty("mail.smtp.port", "SSL的端口号");
详细讲解:http://blog.csdn.net/karem/article/details/4646071
希望能帮到你!