Java 发送邮件

来源:互联网 发布:淘宝店铺全屏海报 编辑:程序博客网 时间:2024/06/05 19:03

1、注册一个网易邮箱,开通smtp服务,作为发送邮件的邮箱服务器





2、jar包准备commons-email-1.3.3.jar,activation.jar等,源码有提供


3、准备工作

//设置smtp host,QQ邮箱邮件服务器为smtp.qq.com,此处为163 邮箱private static final String HostName = "smtp.163.com";private static final String toAdd = "emailtest2016@163.com";//接收人邮箱private static final String toName = "pengyou";//接收人名字private static final String fromName = "研发中心";//发送人名字private static final String fromAdd = "nn4594@163.com";//发送人邮箱//参数是,邮箱服务器的登录用户名,以及密码(测试时写自己的,确保开通了smtp)private static final String username = "nn4594@163.com";private static final String password = "nn4594";//邮件内容编码,防止乱码private static String charset="UTF-8";


4、发送简单邮件

private static void sendText() {//SimpleEmail email = new SimpleEmail();//创建简单邮件,不可附件、HTML文本等//MultiPartEmail  email = new MultiPartEmail();//创建能加附件的邮件,可多个、网络附件亦可//HtmlEmail email = new HtmlEmail();//创建能加附件内容为HTML文本的邮件、HTML直接内联图片但必须用setHtmlMsg()传邮件内容SimpleEmail emailUtil = new SimpleEmail();emailUtil.setCharset(charset);//邮箱服务器smtp host,此处采用自己的QQ邮箱作为邮件服务器emailUtil.setHostName(HostName);try {emailUtil.setSmtpPort(465);//登录邮件服务器的用户名和密码(保证邮件服务器POP3/SMTP服务开启)emailUtil.setAuthenticator(new DefaultAuthenticator(username, password));// 参数是您的真实邮箱和密码emailUtil.setFrom(fromAdd, fromName);emailUtil.addTo(toAdd, "pengyou");emailUtil.addCc("9973341@qq.com");//抄送方emailUtil.addBcc("npy@163.com");//秘密抄送方emailUtil.setSubject("研发中心密码找回");emailUtil.setSSLOnConnect(true);//开启SSL加密//emailUtil.setStartTLSEnabled(true);//开启TLS加密emailUtil.setSubject("主题1");//主题emailUtil.setMsg("本邮件发送仅提供例子,需要您二次开发。" + "pengyou" + ",您的登录密码是:" + "123456asd");emailUtil.send();} catch (Exception e) {e.printStackTrace();}}



5、发送带附件的图片邮件

private static void sendImage() { //创建邮件附件可多个         EmailAttachment attachment = new EmailAttachment();//创建附件  attachment.setPath("images/yun.jpg");  attachment.setDisposition(EmailAttachment.ATTACHMENT);  attachment.setDescription("Picture of John");//附件描述  attachment.setName("yun.jpg");//附件名称  EmailAttachment attachment2 = new EmailAttachment();//创建附件  attachment2.setPath("images/sn.jpg");  attachment2.setDisposition(EmailAttachment.ATTACHMENT);  attachment2.setDescription("Picture of John");//附件描述  attachment2.setName("sn.jpg");//附件名称  MultiPartEmail email = new MultiPartEmail();  try {  email.setHostName(HostName);  email.setAuthenticator(new DefaultAuthenticator(username, password));// 参数是您的真实邮箱和密码  email.addTo(toAdd, toName);  email.setFrom(fromAdd, fromName);  email.setSubject("The picture");  email.setMsg("这是给你的图片");  // add the attachment  email.attach(attachment);  email.attach(attachment2);email.send();} catch (Exception e) {e.printStackTrace();}}

结果:



6、发送HTML文本邮件

private static void sendHtmlEmail(){     HtmlEmail email = new HtmlEmail();     try {                   email.setHostName(HostName);  email.setAuthenticator(new DefaultAuthenticator(username, password));// 参数是您的真实邮箱和密码         email.setHtmlMsg("<html>The apache logo -<a href='https://www.baidu.com'>https://www.baidu.com</a><br><hr> <img src=\"http://b.hiphotos.baidu.com/image/pic/item/500fd9f9d72a6059dc4581122a34349b033bba65.jpg\"></html>");         email.setTextMsg("Your email client does not support HTML messages");         email.addTo(toAdd, toName);  email.setFrom(fromAdd, fromName);      email.setSubject("The picture");         email.send();} catch (Exception e) {e.printStackTrace();}}

结果:




7、源码地址:http://download.csdn.net/detail/u014520797/9425679


0 0
原创粉丝点击