发送邮件代码所需jar的pom文件:
<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>一个简单的发送邮件测试代码:
package com.sun4j.email; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; /** * description: * Created by Sunlight on 2016/10/17. */ public class HtmlEmailTest { public static void main(String[] args) throws EmailException,MalformedURLException{ HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.126.com");//发送服务器的名字 email.setCharset("utf-8");//设置编码集 email.addTo("82068619@qq.com");//收件人邮箱 email.setFrom("xxx@126.com");//发送人邮箱 email.setAuthentication("xxx@126.com", "xxxxx");//发件人的用户名与密码 email.setSubject("这是一封测试邮件,请不要回复!");//发送主题(邮件主题) email.setHtmlMsg(""); email.setMsg("<b><a href=\"http://www.sun4j.com\"> 邮件测试内容</a></b>");//邮件内容 email.send();//发送 } }又或者
package com.sun4.email; import javax.mail.*; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import java.io.UnsupportedEncodingException; import java.util.Properties; import java.util.Random; /** * description: * Created by Sunlight on 2016/10/17. */ public class Test { public static void main(String[] args){ sendEmailCode("82068619@qq.com","1234"); } private static String randomCode() { Random random = new Random(); String numberChar = "0123456789"; StringBuffer numCode = new StringBuffer(); for (int i = 0; i < 4; i++) { // 四位验证码 numCode.append(random.nextInt(numberChar.length())); } return numCode.toString(); } public static void sendEmailCode(String email, String template) { // 配置发送邮件的环境属性 final Properties props = new Properties(); /* * 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host / * mail.user / mail.from */ // 表示SMTP发送邮件,需要进行身份验证 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.126.com"); // 发件人的账号 props.put("mail.user", "xxx@126.com"); // 访问SMTP服务时需要提供的密码 props.put("mail.password", "xxxxxxx"); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 MimeMessage message = new MimeMessage(mailSession); try { // 设置发件人 InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form); String mCode = randomCode(); email = email == null ? "123@qq.com" : email; StringBuilder sbContent = new StringBuilder(); sbContent.append("<p>亲爱的用户,你好!</P>"); sbContent.append(String.format("<p>你的验证码是:%s</P>", mCode)); sbContent.append("<p>此邮件由系统自动发出,请勿直接回复。</P>"); sbContent.append("<p>如果在使用中遇到问题,请发邮件到 service@sun4j.com ,我们将尽快回复。</P>"); sbContent.append("<p>感谢你的访问,祝你使用愉快!</P>"); sbContent.append("<p>www.sun4j.com --专业创新创业服务。</P>"); sbContent.append("<p>Copyright © 2016| Sunlight | All rights reserved.</P>"); template = template == null ? sbContent.toString() : template; // 设置收件人 InternetAddress to = new InternetAddress(email); message.setRecipient(RecipientType.TO, to); // 设置邮件标题 message.setSubject(MimeUtility.encodeText("验证码", "utf-8", "B")); // 生成邮件标题 // 设置抄送 // InternetAddress cc = new InternetAddress("luo_aaaaa@yeah.net"); // message.setRecipient(RecipientType.CC, cc); // 设置密送,其他的收件人不能看到密送的邮件地址 // InternetAddress bcc = new InternetAddress("aaaaa@163.com"); // message.setRecipient(RecipientType.CC, bcc); // 设置邮件标题 // message.setSubject("修改邮箱验证码邮件"); // 设置邮件的内容体 message.setContent(template, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); } catch (AddressException e1) { e1.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } } }