javamail概述

JavaMail

是java提供的一组API,用来发送和接收邮件!

---------------------

邮件相关的协议

smtp 25 --> 简单的邮件传输协议
pop3 110 --> 邮局协议第3版

---------------------


=================================

JavaMail

1. 导包!
* mail.jar
* actvition.jar

----------------

核心类:
1. Session
* 如果你得到了它,表示已经与服务器连接上了,与Connection的作用相似!

得到Session,需要使用Session.getInstance(Properties, Authenticator);
Properites props = new Properties();
props.setProperty("mail.host", "smtp.163.com");
props.setProperty("mail.smtp.auth", "true");

Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("itcast_cxf", "itcast");
}
};
Session session = Session.getInstance(props, auth);

2. MimeMessage
* 它表示一个邮件对象,你可以调用它的setFrom(),设置发件人、设置收件人、设置主题、设置正文!


3. TransPort
* 它只有一个功能,发邮件!

------------------------------------------------------

第一步:获得Session

Session session = Session.getInstance(Properties prop, Authenticator auth);

其中prop需要指定两个键值,一个是指定服务器主机名,另一个是指定是否需要认证!我们当然需要认证!

Properties prop = new Properties();

prop.setProperty(“mail.host”, “smtp.163.com”);//设置服务器主机名

prop.setProperty(“mail.smtp.auth”, “true”);//设置需要认证

 

其中Authenticator是一个接口表示认证器,即校验客户端的身份。我们需要自己来实现这个接口,实现这个接口需要使用账户和密码。

Authenticator auth = new Authenticator() {

    public PasswordAuthentication getPasswordAuthentication () {

        new PasswordAuthentication(“itcast_cxf”, “itcast”);//用户名和密码

}

};

通过上面的准备,现在可以获取得Session对象了:

Session session = Session.getInstance(prop, auth);

 

第二步:创建MimeMessage对象

创建MimeMessage需要使用Session对象来创建:

MimeMessage msg = new MimeMessage(session);

然后需要设置发信人地址、收信人地址、主题,以及邮件正文。

msg.setFrom(new InternetAddress(“itcast_cxf@163.com”));//设置发信人

msg.addRecipients(RecipientType.TO, “itcast_cxf@qq.com,itcast_cxf@sina.com”);//设置多个收信人

msg.addRecipients(RecipientType.CC, “itcast_cxf@sohu.com,itcast_cxf@126.com”);//设置多个抄送

msg.addRecipients(RecipientType.BCC, ”itcast_cxf@hotmail.com”);//设置暗送

msg.setSubject(“这是一封测试邮件”);//设置主题(标题)

msg.setContent(“当然是hello world!”, “text/plain;charset=utf-8”);//设置正文

 

第三步:发送邮件

Transport.send(msg);//发送邮件

 ---------------------------------------------------------------

package javaMail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Test;


public class JavaMail {
@Test
public void fun1() throws AddressException, MessagingException{
//得到session
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.163.com");//设置服务器主机名
prop.setProperty("mail.smtp.auth", "true");//设置需要认证
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("danyuzhu11@163.com", "DANYU1122");
}
};
Session session = Session.getInstance(prop, auth);

//创建MimeMessage
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("danyuzhu11@163.com"));//设置发件人
msg.setRecipients(RecipientType.TO, "494281423@qq.com");//设置收件人
msg.setRecipients(RecipientType.CC, "494281423@qq.com");//设置抄送
msg.setRecipients(RecipientType.BCC, "494281423@qq.com");//设置暗送人
//设置正文
msg.setSubject("测试邮件");
msg.setContent("一峰测试邮件","text/html;charset=utf-8");
//发送邮件
Transport.send(msg);



}

}

 

posted @ 2017-04-28 09:27  贱贱的小帅哥  阅读(125)  评论(0编辑  收藏  举报