现在的位置: 首页 > 综合 > 正文

JavaMail发送邮件

2018年02月13日 ⁄ 综合 ⁄ 共 4274字 ⁄ 字号 评论关闭

项目环境

邮件发送类

package com.entel.research;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

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

public class SendMail
{
	/**
	 * 读邮件模板
	 * author 李建
	 * date 2010-05-14
	 */
	public String readFile(String sourceFile) throws Exception
	{
		File file = new File(sourceFile);
		
		InputStream is = new FileInputStream(file);
		
		byte[] buf = new byte[10240];
		int len = 0;
		String str = null;
		while((len = is.read(buf)) != -1)
		{
			str = new String(buf,0,len);
		}
		return str;
	}
	
	
	/**
	 * 发送邮件
	 * author 李建
	 * date 2010-05-14
	 */
	public boolean sendMail(String smtp, String sendFrom, String sendTo,
			String smtpName, String smtpPassword, String content,String subject)
			throws Exception
	{
		final String _smtpName = smtpName;
		final String _smtpPassword = smtpPassword;

		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", smtp);
		Session session = Session.getInstance(props, new Authenticator()
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication(_smtpName, _smtpPassword);
			}
		});
		session.setDebug(true);

		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress( MimeUtility.encodeText(sendFrom)
				));
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(sendTo));
		msg.setSubject(subject);

		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);
		MimeBodyPart contents = new MimeBodyPart();
		msgMultipart.addBodyPart(contents);

		MimeMultipart bodyMultipart = new MimeMultipart("related");
		contents.setContent(bodyMultipart);
		MimeBodyPart htmlPart = new MimeBodyPart();
		bodyMultipart.addBodyPart(htmlPart);


		htmlPart.setContent(content, "text/html;charset=gbk");

		msg.setHeader("Date", new Date().toString());
		
		//设置发送时间
		msg.setSentDate(new Date());
		msg.saveChanges();

		session.setDebug(false);
		
		try
		{
			Transport.send(msg, InternetAddress.parse(sendTo));
			return true;
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 发送邮件
	 * author 李建
	 * date 2010-05-14
	 */
	public boolean sendMail(String smtp, String sendFrom, String sendTo,
			String smtpName, String smtpPassword, String content,
			String subject, String accessoryAddress, String accessoryName)
			throws Exception
	{
		final String _smtpName = smtpName;
		final String _smtpPassword = smtpPassword;

		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", smtp);
		Session session = Session.getInstance(props, new Authenticator()
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication(_smtpName, _smtpPassword);
			}
		});
		session.setDebug(true);

		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(MimeUtility.encodeText(sendFrom)));
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(sendTo));
		msg.setSubject(subject);
		
		//设置发送时间
		msg.setSentDate(new Date());

		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);
		// 发送content
		MimeBodyPart contents = new MimeBodyPart();
		msgMultipart.addBodyPart(contents);

		MimeMultipart bodyMultipart = new MimeMultipart("related");
		contents.setContent(bodyMultipart);
		MimeBodyPart htmlPart = new MimeBodyPart();
		bodyMultipart.addBodyPart(htmlPart);

		htmlPart.setContent(content, "text/html;charset=gbk");
		// 发送附件
		MimeBodyPart attch1 = new MimeBodyPart();
		msgMultipart.addBodyPart(attch1);
		DataSource ds1 = new FileDataSource(accessoryAddress);
		DataHandler dh1 = new DataHandler(ds1);
		attch1.setDataHandler(dh1);
		attch1.setFileName(MimeUtility.encodeText(accessoryName));

		session.setDebug(false);
		 
		try
		{
			Transport.send(msg, InternetAddress.parse(sendTo));
			return true;
		}
		catch (Exception e) 
		{
			return false;
		}
	}
}

邮件发送测试类

package com.entel.research;

public class JunitMail
{
	public static void main(String[] args)throws Exception
	{
		SendMail sendMail = new SendMail();
		sendMail.sendMail("smtp.qq.com", "xxxxxx@qq.com", "xxxxxx@163.com","xxxxxx", "******", 
					"This is a Test <img src='http://www.hao123.com/images/logox3.gif'>", "subject");
	}
}

抱歉!评论已关闭.