email邮箱发送
//创建htmlemail对象
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");
//设置发送的字符类型
email.setCharset("utf-8");
try {
//设置收件人
email.addTo("邮箱");
//发送人的邮箱为自己的,用户名可以随便填
email.setFrom("邮箱为自己","monster");
//设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
email.setAuthentication("收件人的邮箱","授权码");
//设置发送主题
email.setSubject("测试");
//设置发送内容
email.setMsg("hello word");
//进行发送
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
email实现(二)
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com");
properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("1427876195@qq.com","16位授权码");
}
});
session.setDebug(true);
Transport transport = session.getTransport();
transport.connect("smtp.qq.com","发件人邮箱","16位授权码");
MimeMessage mimeMessage = new MimeMessage(session);
//邮件发送人
mimeMessage.setFrom(new InternetAddress("发件人邮箱"));
mimeMessage.setSubject("测试");
mimeMessage.setContent("安红","text/html;charset=UTF-8");
//邮件接收人
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人邮箱"));
//发送邮件
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//关闭
transport.close();