javaMail
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。
SMTP协议
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式;
SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地;
POP3协议
POP3(Post Office Protocol-Versioon 3)即邮局协议版本3。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件;
邮件收发过程
前期准备
代码实现
public class JavaMail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
senMail("@qq.com", "1234");
}
public static void senMail(String to,String code) throws Exception{
Properties props=new Properties();
props.setProperty("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
Session session=Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication("@163.com", "123456");
}
});
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("@163.com"));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject("激活邮件");
message.setContent("<h1><a href='http://.....?code="+code+">点击进行激活</a><h1>", "text/html;charset=UTF-8");
Transport.send(message);
}
}