Java 邮件发送
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version> </dependency>
/* * Copyright 2016 mljr.com All right reserved. This software is the * confidential and proprietary information of mljr.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with mljr.com . */ package com.mljr.car.league.web.email; /** * @Description * @author 田林(lin.tian@mljr.com) * @date 2016年12月30日 上午11:27:21 */ public class Mail { /** * 序列号 */ private static final long serialVersionUID = -3562218214168975242L; /** * 邮件编码s */ public static final String ENCODEING = "UTF-8"; /** * 服务器地址 */ private String host; /** * 服务器端口号 */ private String portNumber; /** * 发件人的邮箱 */ private String sender; /** * 收件人的邮箱 */ private String receiver; /** * 发件人昵称 */ private String name; /** * 账号 */ private String username; /** * 密码 */ private String password; /** * 主题 */ private String subject; /** * 信息(支持HTML) */ private String message; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getSender() { return sender; } public String getPortNumber() { return portNumber; } public void setPortNumber(String portNumber) { this.portNumber = portNumber; } public void setSender(String sender) { this.sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
MailUtil
/* * Copyright 2016 mljr.com All right reserved. This software is the * confidential and proprietary information of mljr.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with mljr.com . */ package com.mljr.car.league.web.email; import java.util.Date; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import com.mljr.car.league.erp.api.dto.BusinessOpportunityDTO; //import org.apache.log4j.Logger; /** * @Description * @author 田林(lin.tian@mljr.com) * @date 2016年12月30日 上午11:27:56 */ public class MailUtil { public static boolean send(Mail mail) { //发送email对象 HtmlEmail email = new HtmlEmail(); try { //这里是SMTP发送服务器的名字 email.setHostName(mail.getHost()); //端口号不为空时,用户自定义的端口号为SMTP发送服务器端口号 if (!"".equals(mail.getPortNumber())) { email.setSSLOnConnect(true); email.setSslSmtpPort(mail.getPortNumber()); } //字符编码集的设置 email.setCharset(Mail.ENCODEING); //收件人的邮箱 email.addTo(mail.getReceiver()); //发送人的邮箱 email.setFrom(mail.getSender(), mail.getName()); // 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码 email.setAuthentication(mail.getUsername(), mail.getPassword()); // 要发送的邮件主题 email.setSubject(mail.getSubject()); // 要发送的信息,由于使用了HtmlEmail,可以在邮件内容中使用HTML标签 email.setMsg(mail.getMessage()); // 发送 email.send(); return true; } catch (EmailException e) { e.printStackTrace(); return false; } } }
Test
/* * Copyright 2016 mljr.com All right reserved. This software is the * confidential and proprietary information of mljr.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with mljr.com . */ package com.mljr.car.league.web.email; /** * @Description * @author 田林(lin.tian@mljr.com) * @date 2016年12月30日 上午11:27:02 */ public class Test { public static void main(String[] args) { Mail mail = new Mail(); mail.setHost("smtp.exmail.qq.com"); //设置邮件服务器,如果不用QQ邮箱的,自己找找看相关的 mail.setPortNumber("465"); //设置邮件服务器端口号,默认25 mail.setSender("***@mljr.com"); //发送人 mail.setName("***"); //发送人昵称 mail.setReceiver("****@qq.com"); //接收人 mail.setUsername("****@mljr.com"); //登录账号,一般都是和邮箱名一样 mail.setPassword("****"); //QQ邮箱登录第三方客户端时,密码框请输入“授权码”进行验证。其他的密码具体查看邮件服务器的说明 mail.setSubject("新加盟商"); mail.setMessage("<h1>内容</h1>"); if (new MailUtil().send(mail)) { System.out.println("发送成功"); } else { System.out.println("发送失败"); } } }
参考:http://www.jianshu.com/p/5ba3bde60f21
http://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html