今天使用了MailServer自定义邮件服务器和Foxmail进行代码发送邮件测试。具体邮箱服务器和foxmail的安装步骤非常简单就不进行记录了,安装包啥的可以直接到官网进行下载或者联系我邮箱。
public class SendEmailUtils {
public static void main(String[] args) {
send("user01@cupid.com", ProduceUtils.getCode() + ProduceUtils.getCode());
}
public static void send(String toMail , String code){
try {
Properties props = new Properties();
//由于是本地邮箱服务器就不需要填写了
//props.setProperty("host", "smtp");
EmailEntity entity = new EmailEntity("user05@cupid.com", "123");
Session session = Session.getInstance(props, entity);
//创建一个邮件对象
Message message = new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress("user05@cupid.com"));
//设置收件人
message.addRecipient(RecipientType.TO, new InternetAddress(toMail));
//设置主题
message.setSubject("来自于丘比特求爱网的注册验证消息");
//设置邮件正文
message.setContent("<h1>来自于丘比特求爱网的注册验证消息,请点击以下链接</h1><h3><a href='http://localhost:8080/JMailDemo/CheckMailServlet?code="+code+"'>http://localhost:8080/JMailDemo/CheckMailServlet?code="+ code +"</a></h3>" , "text/html;charset=UTF-8");
//发送邮件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}