Last active
December 16, 2015 06:08
-
-
Save wdfx100/5389036 to your computer and use it in GitHub Desktop.
发送电子邮件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jar:commons-email-1.2 | |
//1.5的javaee.jar下的mail文件删除,添加1.4的mail.jar | |
//1、commons-email发送普通文本邮件 | |
public static void sendSimpleEmail(String toEmailAddress,String subject,String msg){ | |
Email email = new SimpleEmail(); | |
email.setHostName("smtp.163.com");//发信件的邮件服务器 | |
email.setAuthentication("wdfx100", "**********");//设置身份验证:用户名和密码 | |
email.setCharset("UTF-8"); | |
email.setTLS(true);//加密方式进行身份验证 | |
try { | |
email.setFrom("[email protected]"); | |
email.setSubject(subject); | |
email.setMsg(msg); | |
email.addTo(toEmailAddress); | |
email.send(); | |
} catch (EmailException e) { | |
throw new DataAccessException("邮件异常",e); | |
} | |
} | |
//2、commons-email发送HTML邮件 | |
public static void sendHtmlEmail(String toEmailAddress,String subject,String htmlMsg){ | |
Email email = new SimpleEmail(); | |
email.setHostName("smtp.163.com");//发信件的邮件服务器 | |
email.setAuthentication("wdfx100", "**********");//设置身份验证:用户名和密码 | |
email.setCharset("UTF-8"); | |
email.setTLS(true);//加密方式进行身份验证 | |
try { | |
email.setFrom("[email protected]"); | |
email.setSubject(subject); | |
email.setMsg(htmlMsg); | |
email.addTo(toEmailAddress); | |
email.send(); | |
} catch (EmailException e) { | |
throw new DataAccessException("邮件异常",e); | |
} | |
} | |
//3、commons-email发送带附件的邮件 | |
public static void sendMultipartEmail(String path,String toEmailAddress,String msg){ | |
EmailAttachment ea = new EmailAttachment(); | |
ea.setPath(path);//发送附件的路径 | |
MultiPartEmail email = new MultiPartEmail(); | |
email.setHostName("smtp.qq.com"); | |
email.setAuthentication("**********","**********"); | |
email.setCharset("UTF-8"); | |
email.setTLS(true); | |
try { | |
email.setFrom("[email protected]"); | |
email.setMsg(msg); | |
email.addTo(toEmailAddress); | |
email.attach(ea); | |
email.send(); | |
} catch (EmailException e) { | |
throw new DataAccessException("邮件异常",e); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment