JAVAMail简介
javaMail是sun公司提供的API之一,是处理电子邮件相关的编程接口。但javaMail没有附加到标准java开发工具包中,要使用javaMail许到官网下载jar包。
http://www.oracle.com/technetwork/java/javamail/index.html
常见的邮件协议有:
SMTP:简单邮件传输协议,是用于发生邮件的,端口是25;
POP3:邮局协议版本3,用于接收邮件的。端口是110;
IMAP:交互式邮件存取协议,也是用于接收邮件的协议,端口号是143
JAVAMail常用类
javax.mail.Session
session为会话类,表示连接工厂,也可以处理配置设置与身份验证。
创建session时可用session的静态方法:
public static Session getDefaultInstance(java.util.Properties props)
public static Session getDefaultInstance(java.util.Properties props,Authenticator authenticator)
public static Session getInstance(java.util.Properties props)
public static Session getInstance(java.util.Properties props,Authenticator authenticator)
Properties参数为配置文件类,为session设置主机名,协议等。
Authenticator参数是身份验证信息。
javax.mail.Authenticator
身份验证类,是一个抽象类,有一个方法protected PasswordAuthentication getPasswordAuthentication,返回为身份验证信息。
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pac= new PasswordAuthentication(“String name”, "String password");
return pac;
}
}
javax.mail.internet.Message
消息类,Message是一个抽象类,添加正文标题等。
MimeMessage是Message的一个实现类:
MimeMessage message=new MimeMessage(session); //构造方法
public void setSubject(java.lang.String subject); //设置标题,可有第二个参数设置编码方式
public void setContent(Multipart mp); //设置正文,参数也可是String
public void setText(java.lang.String text, java.lang.String charset); //也是设置正文的,第二个参数可无
public void setFrom(Address address); //设置收件人地址
public void setRecipient(Message.RecipientType type, Address address); //设置收件人地址
其中RecipientType定义了三个值:
- Message.RecipientType.TO 收件人
- Message.RecipientType.CC 抄送
- Message.RecipientType.BCC 密件抄送
javax.mail.Transport
消息传输类,此类使用SMTP协议发送邮件
public static void send(Message msg) //发送邮件
JAVAMail发送普通邮件
直接上代码:
public static void main(String[] args) throws Exception {
System.out.println("start");
//建立服务器与本地的链接
Properties props=new Properties();
//设置邮件服务器主机名 ,QQ邮箱的是smtp.qq.com
props.setProperty("mail.host","smtp.qq.com");
//设置协议
props.setProperty("mail.transport.protocol", "smtp");
//指定身份验证,是否采取身份验证,默认为false
props.setProperty("mail.smtp.auth", "true");
//开始ssl加密,不然会出错
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
//需设置为true,不然报错530
props.put("mail.smtp.ssl.enable", "true");
//获取邮箱会话,第一个参数是链接,第二个是认证信息(匿名类)
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//第一个是邮箱地址,第二个为授权码
PasswordAuthentication pac= new PasswordAuthentication("1036595195", "wqmuegjuuuyqbcih");
return pac;
}
});
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress("1036595195@qq.com"));
//设置收件人
message.setRecipient(RecipientType.CC, new InternetAddress("1036595195@qq.com"));
//主题
message.setSubject("测试");
//内容
//message.setContent("成功","text/html;charset=utf-8");
message.setText("测试邮件一封");
Transport.send(message);
System.out.println("success");
}
新建Properties对象时可将配置写入键值对配置文件中,以减少代码量。
使用配置文件构造Properties对象代码为:
InputStream proStream = MailTest.class.getResourceAsStream("/email_configuration.properties"); //创建流文件
Properties props=new Properties();
props.load(proStream); //读取流文件
需要注意的时文件的路径。
JAVAMail发送带附件邮件
代码:
public static void main(String[] args) throws Exception {
System.out.println("start");
//建立服务器与本地的链接
//InputStream proStream = MailTest.class.getResourceAsStream("/email_configuration.properties");
Properties props=new Properties();
//props.load(proStream);
//设置邮件服务器主机名
props.setProperty("mail.host","smtp.qq.com");
//协议
props.setProperty("mail.transport.protocol", "smtp");
//指定身份验证,采用用户名密码认证
props.setProperty("mail.smtp.auth", "true");
//开启ssl加密,不然会出错
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
//创建会话
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//第一个是邮箱地址,第二个为授权码,授权码得登陆邮箱获取,下面会讲到
PasswordAuthentication pac= new PasswordAuthentication("1036595195", "*******");
return pac;
}
});
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress("1036595195@qq.com"));
//设置收件人
message.setRecipient(Message.RecipientType.CC, new InternetAddress("2323708444@qq.com"));
//主题
message.setSubject("测试");
//创建BodyPart
BodyPart messageBodyPart = new MimeBodyPart();
//设置正文
messageBodyPart.setText("This is message body");
//创建Multipart对象
Multipart multipart = new MimeMultipart();
// 将BodyPart对象放到Multipart中作为正文
multipart.addBodyPart(messageBodyPart);
//创建第二个BodyPart对象添加附件
messageBodyPart = new MimeBodyPart();
String filename = "a.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
//设置附件的名称
messageBodyPart.setFileName(filename);
//将BodyPart对象放到Multipart中作为附件
multipart.addBodyPart(messageBodyPart);
// 将multipart传给message
message.setContent(multipart);
// 发送邮件
Transport.send(message);
System.out.println("success");
}
添加附件与发普通的基本相似,发生简单邮件时设置的正文是直接setText(String value)或setContent(Stringvaluevalue)方法
而发生附件时是利用setContent(Multipart mp)设置 Multipart类,而Multipart类通过addBodyPart(BodyPart part)方法添加多个BodyPart类,BodyPart中通过setText(String text)方法设置正文,通过setDataHandler(DataHandler dh)方法设置附件。
另:DataHandler的构造方法为:
public DataHandler(DataSource ds) //构造DataHandler时传入一个DatasSource对象
JAVAMail异常
以QQ邮箱为例,使用JAVAMail发送邮寄时需要登录邮箱到设置的账户中开启POP3/SMTP服务,同时生成授权码。
如果没有开启POP3/SMTP服务是无法发送成功的。
530 Error: A secure connection is requiered(such as ssl).
需要一个ssl协议加密,解决方法:
props.put("mail.smtp.ssl.enable", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
503 Error: need EHLO and AUTH first !
没有设置身份验证,默认为false的,解决方法:
props.setProperty("mail.smtp.auth", "true");
535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´:
授权码错误!!