163邮件发送工具类 package com.union.util;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import java
package com.union.util; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailUtil { /** * 发送邮件 参数一:发送邮件给谁 * 参数二:发送邮件的内容 * 邮箱类型:163邮箱 */ public static void sendMail(String toEmail, String emailMsg) throws Exception { // 创建Properties 对象 Properties props = new Properties(); // 添加smtp服务器属性 props.put("mail.smtp.host", "smtp.163.com"); // 需要验证 props.put("mail.smtp.auth", "true"); Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { // 用户名,密码 return new PasswordAuthentication("mick_ming", "mick_ming@163com"); } }; Session session = Session.getInstance(props, auth); // 2_创建一封邮件 Message message = new MimeMessage(session); // 发送人 message.setFrom(new InternetAddress("mick_ming@163.com")); message.setRecipient(RecipientType.TO, new InternetAddress(toEmail)); // 标题 message.setSubject("联合数据电子发票"); // 样式,编码 message.setContent(emailMsg, "text/html;charset=UTF-8"); // 3_发送邮件 Transport.send(message); } /** 测试类 */ public static void main(String[] args) throws Exception { // 接收人 String toEmail = "mingdj@union01.com"; // 邮件内容 String emailMsg = "测试一下123"; sendMail(toEmail, emailMsg); System.out.println("发送成功。。。"); } }