javamail发送邮件

1. jar包:

 mail.jar

2. java 类

package com.yoplore.apollo.wanwei;

import java.util.*;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

public class SendEmail{
	public void sendEmail(String senderQQ,String senderQQAuthorizationCode,String recipientQQMail,String ccListQQMail,String mailTitle,String station_name,String station_code ) throws Exception{
		//拼接发件人QQ邮箱地址
		String senderQQEmail = senderQQ + "@qq.com";
		
		Properties prop = new Properties();
		// 开启debug调试,以便在控制台查看
		prop.setProperty("mail.debug", "false");
		// 设置邮件服务器主机名
		prop.setProperty("mail.host", "smtp.qq.com");
		// 发送服务器需要身份验证
		prop.setProperty("mail.smtp.auth", "true");
		// 发送邮件协议名称
		prop.setProperty("mail.transport.protocol", "smtp");
		//使用SSL,企业邮箱必需!
		//开启安全协议
		// 开启SSL加密,否则会失败

		MailSSLSocketFactory sf = null;
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		//
//		//获取Session对象
//		Session s = Session.getDefaultInstance(prop,new Authenticator() {
//			//此访求返回用户和密码的对象
//			@Override
//			protected PasswordAuthentication getPasswordAuthentication() {
//				PasswordAuthentication pa = new PasswordAuthentication("wygm@daee.cn", "Ok1234");
//				return pa;
//			}
//		});
//		//设置session的调试模式,发布时取消
//		s.setDebug(true);

		// 创建session
		Session session = Session.getInstance(prop);
		// 通过session得到transport对象
		Transport ts;
		try {
			ts = session.getTransport();
			// 连接邮件服务器:邮箱类型,帐号,授权码
			// qq邮箱授权码获取:登录QQ邮箱 → 设置 → 账户 → 找到IMAP/SMTP服务,点击开启 ,开启后会获取一个授权码
			
			// 创建邮件
			Message message = new MimeMessage(session);
			// 指明邮件的发件人
			message.setFrom(new InternetAddress(senderQQEmail));
			// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
			//	Message.RecipientType.TO 收件人
			//	Message.RecipientType.CC 抄送人
			//	Message.RecipientType.BCC 暗送人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientQQMail));
			message.setRecipient(Message.RecipientType.CC, new InternetAddress(ccListQQMail));
			// 邮件的标题
			message.setSubject(mailTitle);

			MimeBodyPart text = new MimeBodyPart();
			text.setContent("<p>王工,你好~<br/></p>我是远索科技有限公司的工程师<br/>我们新引入了一个电站数据,电站数据如下:<br/>"+"  ① 电站名称:"+station_name+",电站编码:"+station_code, "text/html;charset=UTF-8");
//			// 准备图片数据
//			MimeBodyPart image = new MimeBodyPart();
//			DataHandler dh = new DataHandler(new FileDataSource("E:\\1.png"));
//			image.setDataHandler(dh);
//			image.setContentID("xxx.jpg");

			// 附件部分
//			MimeBodyPart messageBodyPart = new MimeBodyPart();
//			String filename = "E:\\1.png";
//			DataSource source = new FileDataSource(filename);
//			messageBodyPart.setDataHandler(new DataHandler(source));
//			messageBodyPart.setFileName(filename);


			// 描述数据关系
			MimeMultipart mm = new MimeMultipart();
			mm.addBodyPart(text);
//			mm.addBodyPart(image);
//			mm.addBodyPart(messageBodyPart);
			mm.setSubType("related");

			message.setContent(mm);
			message.saveChanges();
			// 发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			throw e;
		}

	}
}

  3. 调用java 类

try {
	//添加成功,发送邮件通知
	SendEmail email = new SendEmail();
	email.sendEmail(Configs.senderQQ, Configs.senderQQAuthorizationCode, Configs.recipientQQMail, Configs.CcListQQMail, Configs.mailTitle,station_name,station_code);	
	cmdRes = ConstantUtil.CMD_RES_SUCCESS;
	resFailReason = newMap.get("failreason").toString();
} catch (Exception e) {
	cmdRes = ConstantUtil.CMD_RES_FAIL;
	resFailReason = "添加成功,邮件通知失败,请联系相关人员,手动发送邮件通知第三方!";
}

  4. 配置变量

public static final String senderQQ = "******";				  		  //发件人的QQ号
public static final String senderQQAuthorizationCode = "**********";		       //发件人的QQ授权码,
public static final String recipientQQMail = "*****@yeah.net";		  		   //收件人的QQ邮箱
public static final String CcListQQMail = "******@qq.com";   		  		  //抄送人的QQ邮箱
public static final String mailTitle = "关于 引、泄水流量数据接入";				   //发送邮件主题

  5. 关于发件人的QQ授权码获取步骤

  qq邮箱授权码获取:登录QQ邮箱 → 设置 → 账户 → 找到IMAP/SMTP服务,点击开启 ,开启后会获取一个授权码

 

 

posted @ 2019-08-13 15:53  小小飞郭  阅读(216)  评论(0编辑  收藏  举报