您当前的位置: 首页 >  童心同萌 Java

Java发送邮件

童心同萌 发布时间:2018-11-22 12:00:08 ,浏览量:3

外网用户可以通过QQ邮箱服务器发送邮件



	com.sun.mail
	javax.mail
	1.6.1
e.account=123412341234@qq.com
e.pass=123412341234
e.host=smtp.qq.com
e.port=465
e.protocol=smtp
e.mailssl=false
e.nickname=ehdp
package com.jac.common.utils;
 
import java.util.Properties;
 
import javax.mail.Message;
import javax.mail.MessagingException;
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.apache.commons.lang.StringUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;
 
import com.jac.entity.security.User;
 
public class SendEmailUtil {
	
	private static String ACCOUNT;	//登录用户名
	private static String PASS;	//QQ邮箱授权码 避免直接显示密码
	private static String HOST;	//服务器地址(邮件服务器)
	private static String PORT;	//端口
	private static String PROTOCOL;	//协议
	private static String MAILSSL;	//安全协议
	private static String NICKNAME;	//发件人别名
 
	static{
		Properties prop = new Properties();
		try {
			prop = PropertiesLoaderUtils.loadAllProperties("email.properties");//生产环境
		} catch (Exception e) {
			System.out.println("加载属性文件email.properties失败");
			e.printStackTrace();
		}
		ACCOUNT = prop.getProperty("e.account");
		PASS = prop.getProperty("e.pass");
		HOST = prop.getProperty("e.host");
		PORT = prop.getProperty("e.port");
		PROTOCOL = prop.getProperty("e.protocol");
		MAILSSL = prop.getProperty("e.mailssl");
		NICKNAME = prop.getProperty("e.nickname");
	}
	
	public static void sendEmail(String toAddress) throws AddressException,MessagingException {
		Properties properties = new Properties();
		properties.put("mail.transport.protocol", PROTOCOL);// 连接协议
		properties.put("mail.smtp.host", HOST);// 主机名
		properties.put("mail.smtp.port", PORT);// 端口号
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 连接QQ邮箱服务器时需要开启
		properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
		// 得到回话对象
		Session session = Session.getInstance(properties);
		// 获取邮件对象
		Message message = new MimeMessage(session);
		// 设置发件人邮箱地址
		message.setFrom(new InternetAddress(ACCOUNT));
		// 设置收件人邮箱地址
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
		// 设置邮件标题
		message.setSubject("审批任务");
		// 设置邮件的内容体
		message.setContent("您有新的审批任务需要处理!", "text/html;charset=UTF-8");
		// 得到邮差对象
		Transport transport = session.getTransport();
		// 连接自己的邮箱账户
		transport.connect(ACCOUNT, PASS);// PASS为QQ邮箱开通的stmp服务后得到的客户端授权码
		// 发送邮件
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}
	
}
SendEmailUtil.sendEmail("123412341234@qq.com");    //目标邮箱 可以不是QQ邮箱

内网用户只能根据内网服务器邮箱的配置向内网邮箱地址发送邮件

e.protocol=smtp
e.host=smtp.firstronix.com
e.port=25
e.account=123412341234@firstronix.com
e.pass=123412341234
e.mailssl=false
e.nickname=ehdp
package com.jac.common.utils;
 
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
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 org.apache.commons.lang.StringUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.jac.entity.security.User;
import com.sun.mail.util.MailSSLSocketFactory;
 
/**
 * 该类为邮件封装类
 * 设置邮件收发人,内容,日期,以及发送功能
 * @author changwq
 */
public class SendEmailUtil {
	
	private static String ACCOUNT;	//登录用户名
	private static String PASS;	//登录密码
	private static String HOST;	//服务器地址(邮件服务器)
	private static String PORT;	//端口
	private static String PROTOCOL;	//协议
	private static String MAILSSL;	//安全协议
	private static String NICKNAME;	//发件人别名

	static{
		Properties prop = new Properties();
		try {
		prop = PropertiesLoaderUtils.loadAllProperties("email.properties");//生产环境
		} catch (Exception e) {
		System.out.println("加载属性文件email.properties失败");
		e.printStackTrace();
		}
		ACCOUNT = prop.getProperty("e.account");
		PASS = prop.getProperty("e.pass");
		HOST = prop.getProperty("e.host");
		PORT = prop.getProperty("e.port");
		PROTOCOL = prop.getProperty("e.protocol");
		MAILSSL = prop.getProperty("e.mailssl");
		NICKNAME = prop.getProperty("e.nickname");
	}
	
	/**
	* 发送邮件
	* @param to		接收人
	* @param subject	标题
	* @param msgtext	内容
	*/
	public static void sendEmail(String toEmail,String subject,String msgtext) throws Exception {
		
		Properties properties = new Properties();
		//设置smtp协议 
		properties.put("mail.transport.protocol", PROTOCOL); 
		//设置服务器连接地址
		properties.put("mail.smtp.host", HOST);
		//设置默认端口号
		properties.put("mail.smtp.port", PORT);
		//设置TLS保护连接,默认为false
		properties.put("mail.smtp.starttls.enable", "true");
		//设置身份校验
		properties.put("mail.smtp.auth", "true");
		//开启安全协议 使用SSL,企业邮箱必需!
		MailSSLSocketFactory sf = null;
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
		} catch (GeneralSecurityException e1) {
			e1.printStackTrace();
		}
		if ("true".equalsIgnoreCase(MAILSSL)) {
			properties.put("mail.smtp.ssl.enable", "true");
			properties.put("mail.smtp.ssl.socketFactory", sf);
		}
		
		//使用环境属性和授权信息,创建邮件会话
		Session session = Session.getDefaultInstance(properties);
		//控制台打印日志
		session.setDebug(true);
		
		//创建邮件消息
		Message msg = new MimeMessage(session);
		//设置发件人的邮箱
		msg.setFrom(new InternetAddress(ACCOUNT, NICKNAME));
		//设置收件人的邮箱
		msg.setRecipient(RecipientType.TO, new InternetAddress(toEmail));
		//设置邮件标题
		msg.setSubject(subject);
		//设置发送时间
		msg.setSentDate(new Date());
		//设置邮件的内容体
		Multipart multipart = new MimeMultipart();
		MimeBodyPart mbp = new MimeBodyPart();
		mbp.setContent(msgtext, "text/html;charset=utf-8");
		multipart.addBodyPart(mbp);
		//添加附件
		/*MimeBodyPart body = new MimeBodyPart();
		body.attachFile(fileStr);
		multipart.addBodyPart(body);*/
		msg.setContent(multipart);
		msg.saveChanges();
		
		//建立邮件传输对象
		Transport transport = session.getTransport(PROTOCOL);
		//与服务端建立连接
		transport.connect(HOST,ACCOUNT,PASS);
		//发送邮件
		transport.sendMessage(msg, msg.getAllRecipients());
		//关闭
		transport.close();
	}
	
	public static StringBuilder getEmailText(String emailContext, String displayName, User operator) {
		String fullname = operator.getFullname();
		String phone = operator.getPhone();
		if (StringUtils.isEmpty(phone)) {
			phone = "----";
		}
		StringBuilder emailContent = new StringBuilder("审批任务		" +
				".container{			font-family: 'Microsoft YaHei';			width: 600px;			margin: 0 auto;			padding: 8px;" +
				"			border: 3px dashed #db303f;			border-radius: 6px;		}		.title{			text-align: center;			" +
				"color: #db303f;		}		.content{			text-align: justify;			color: #717273;			font-weight: 600;		}  " +
				"	  footer{			text-align: right;			color: #db303f;			font-weight: 600;			font-size: 18px;		}" +
				"
" + displayName + "

" + emailContext + "

" +"联系" + fullname + ":" + phone + "
"); return emailContent; } }
	//邮件发送
	try {
		SendEmailUtil.sendEmail(userManager.findUserByName(request.getParameter(nextOperator)).getEmail(), "审批任务", SendEmailUtil.getEmailText("您有新的审批任务需要处理!",activeTask.getDisplayName(),ShiroUtils.getUser()).toString());
	} catch (Exception e) {
		// TODO: handle exception
	}

关注
打赏
1688896170
查看更多评论

童心同萌

暂无认证

  • 3浏览

    0关注

    87博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2097s