package com.minstone.message.util; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.springframework.stereotype.Component; @Component public class SendmailUtil { // 设置服务器 private static String KEY_SMTP = "mail.smtp.host"; // private static String VALUE_SMTP = "smtp.163.com"; // private static String VALUE_SMTP = "smtp.qq.com"; // private static String VALUE_SMTP = "smtp.exmail.qq.com"; private static String VALUE_SMTP = "smtp.sina.com.cn"; // 服务器验证 private static String KEY_PROPS = "mail.smtp.auth"; private static boolean VALUE_PROPS = true; // 发件人用户名、密码 // private String SEND_USER = "abc25660663"; // private String SEND_USER = "1481596361"; // private String SEND_USER = "hongxy"; private String SEND_USER = "mingdong123abc"; //发件人邮箱 // private String FROMADDRESS=SEND_USER+"@163.com"; // private String FROMADDRESS=SEND_USER+"@qq.com"; // private String FROMADDRESS=SEND_USER+"@minstone.com.cn"; private String FROMADDRESS=SEND_USER+"@sina.com"; private String SEND_UNAME = "政务服务中心"; //邮箱密码 // private String SEND_PWD = "25660663abc."; // private String SEND_PWD = "313131abc."; // private String SEND_PWD = "25660663Abc."; private String SEND_PWD = "mingdong123abc"; // 建立会话 private MimeMessage message; private Session s; /* * 初始化方法 */ public SendmailUtil() { Properties props = System.getProperties(); // 连接协议 props.put("mail.transport.protocol", "smtp"); props.put(KEY_SMTP, VALUE_SMTP); props.put(KEY_PROPS, "true");//true一定要加双引号 props.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用 props.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息 s = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SEND_UNAME, SEND_PWD); }}); s.setDebug(true); message = new MimeMessage(s); } /** * 发送邮件 * * @param headName * 邮件头文件名 * @param sendHtml * 邮件内容 * @param receiveUser * 收件人地址 */ public void doSendHtmlEmail(String headName, String sendHtml, String receiveUser){ try { // 发件人 InternetAddress from = new InternetAddress(FROMADDRESS);//发送人地址 message.setFrom(from); // 收件人 InternetAddress to = new InternetAddress(receiveUser); message.setRecipient(Message.RecipientType.TO, to); // 邮件标题 message.setSubject(headName); String content = sendHtml.toString(); // 邮件内容,也可以使纯文本"text/plain" message.setContent(content, "text/html;charset=UTF-8"); message.setSentDate(new Date()); message.saveChanges(); Transport transport = s.getTransport("smtp"); //这种方式应用于163网易邮箱和QQ邮箱,最后一个参数是授权码,网易的授权码可以自动设置,而QQ邮箱的授权码需通过短信才可收到 // transport.connect(FROMADDRESS,"25660663abc"); // transport.connect(FROMADDRESS,"yigoehcqlbxkhfid"); //QQ企业邮箱,新浪邮箱,由于企业QQ邮箱和新浪邮箱开通POP3/SMTP/IMAP服务时,不需要授权码 transport.connect(VALUE_SMTP, FROMADDRESS, SEND_PWD); // 发送 transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("send success!"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } public static void main(String[] args) { SendmailUtil se = new SendmailUtil(); se.doSendHtmlEmail("邮件标题1111", "邮件内容", "1481596361@qq.com"); } }
以上例子是JavaMail实现邮箱之间发送邮件功能,需要注意的是发送邮箱是否需要授权码,需要授权码书写的方式和不需要授权码书写的方式不一致,主要区别在
transport.connect方法的参数,而接收邮件的邮箱却不规定,任意邮箱都可以。
需要导入一些包:
具体根据自身的情况进行导入,一般来说mail.jar或者javax.mail.jar和activation.jar需导入