使用java实现163邮件发送
登陆163邮箱,进行如下设置:
使用前必须先去注册163邮箱(免费),还有去设置-->客户端授权密码-->开启(然后自己设置一个密码),在去设置-->POP3/SMTP/IMAP-->全选√.
测试代码如下:
package me.gacl.mail.create;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SendMail {
private String from = "[email protected]";// 发件人的邮箱地址
private String user = "[email protected]";// 发件人称号,同邮箱地址
private String password = "";// 发件人邮箱客户端的授权码
/* 发送验证信息的邮件 */
public boolean sendMail(String to, String text, String title) {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.163.com"); // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", "smtp.163.com"); // 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true"); // 用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props); // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
// 用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true); // 用session为参数定义消息对象
MimeMessage message = new MimeMessage(session); // 加载发件人地址
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 加载收件人地址
message.setSubject(title); // 加载标题
Multipart multipart = new MimeMultipart(); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
BodyPart contentPart = new MimeBodyPart(); // 设置邮件的文本内容
contentPart.setContent(text, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
message.setContent(multipart);
message.saveChanges(); // 保存变化
Transport transport = session.getTransport("smtp"); // 连接服务器的邮箱
transport.connect("smtp.163.com", user, password); // 把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) { // 做测试用
SendMail sm = new SendMail();
sm.sendMail("[email protected]", "你好,这是一封测试邮件,无需回复。", "测试邮件");
}
}
借鉴来源: