javaMail的maven坐标:
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
阿里云邮箱默认是开启了smtp,所以无需在阿里云邮箱设置这个(目前是这样的,2016.10.19);163邮箱普通用户是不能通过smtp协议发送邮件,除非是vip用户。
发送邮件代码:
package com.day24.JavaMail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailTestForAliYunExample {
public static void main(String[] args) throws MessagingException {
Properties prop=new Properties();
prop.put("mail.host","smtp.aliyun.com" );
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
//使用java发送邮件5步骤
//1.创建sesssion
Session session=Session.getInstance(prop);
//开启session的调试模式,可以查看当前邮件发送状态
session.setDebug(true);
//2.通过session获取Transport对象(发送邮件的核心API)
Transport ts=session.getTransport();
//3.通过邮件用户名密码链接,阿里云默认是开启个人邮箱pop3、smtp协议的,所以无需在阿里云邮箱里设置
ts.connect("XXXX@aliyun.com", "password");
//4.创建邮件
Message msg=createSimpleMail(session);
//5.发送电子邮件
ts.sendMessage(msg, msg.getAllRecipients());
}
public static MimeMessage createSimpleMail(Session session) throws AddressException,MessagingException{
//创建邮件对象
MimeMessage mm=new MimeMessage(session);
//设置发件人
mm.setFrom(new InternetAddress("XXXX@aliyun.com"));
//设置收件人
mm.setRecipient(Message.RecipientType.TO, new InternetAddress("targetUser@163.com"));
//设置抄送人
mm.setRecipient(Message.RecipientType.CC, new InternetAddress("XXXX@qq.com"));
mm.setSubject("XXX网站注册邮件!");
mm.setContent("验证码为690", "text/html;charset=gbk");
return mm;
}
}