公司因为业务需要,需要实现java发送邮件功能,不同的邮箱相关代码有所不同,特记录,方便下次使用
QQ邮箱如下:QQ邮箱是做的比较好的一个,对于密码不是明文保存,而是需要用户在QQ邮箱内生成对应的授权码,将授权码写入到密码当中
// 附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
<span style="white-space:pre"> </span>//CreateFile()为自定义方法,返回一个文件对象,做附件用
attachment.setPath(CreateFile());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
// smtp host
email.setHostName("smtp.qq.com");
// 端口号
email.setSmtpPort(587);
// 登陆邮件服务器的用户名和密码
email.setAuthentication("97504188@qq.com", "cabtijswujlsb");
// 接收人
email.addTo("97504188@qq.com");
// 发送人
email.setFrom("97504188@qq.com", name);
// 标题
email.setSubject(name);
// 邮件内容
email.setMsg("--------------------");
// 添加附件
email.attach(attachment);
// 发送
email.send();
// 附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(CreateFile(list, listacc, dir, name));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
// smtp host
email.setHostName("smtp.139.com");
// 登陆邮件服务器的用户名和密码
email.setAuthentication("13549504275@139.com", "123456789");
// 接收人
email.addTo("4448466@qq.com");
// 发送人
email.setFrom("13549504275@139.com", name);
// 标题
email.setSubject(name);
// 邮件内容
email.setMsg("--------------------");
// 添加附件
email.attach(attachment);
// 发送
email.send();