java实现邮件发送(带附件)

JAVA实现发送邮件,可以同时发送附件,支持主流的邮件服务商比如163,126等,需要注意要开启smtp服务(网易的貌似需要绑定手机号才能开通)。

邮件内容如果是HTML文本,则自动会转为网页哦!

</pre><pre name="code" class="java">EmailUtils2.java

 
package com.xxxxx.xxxxx;  //自己把包名改成你项目的

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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 javax.mail.internet.MimeUtility;

/**
 * 发送邮件工具类
 * 
 * @author DingJiaCheng
 * */
public class EmailUtils2 {
	private MimeMessage message;
	private Session session;
	private Transport transport;

	private String mailHost = "";
	private String sender_username = "";
	private String sender_password = "";

	private Properties properties = new Properties();

	/**
	 * 发送邮件
	 * 
	 * @param subject		邮件主题
	 * @param sendHtml  	邮件内容
	 * @param receiveUser	收件人地址
	 * @param attachment 	附件
	 * @return boolean		发送邮件请求的状态,true为请求成功,否则请求失败
	 */
	public static boolean send(String subject, String text,
			String receiver, File file) {
		EmailUtils2 eu = new EmailUtils2(true);
		boolean b = eu.doSendHtmlEmail(subject, text, receiver, file);
		return b;
	}
	
	
	/*
	 * 初始化方法
	 */
	public EmailUtils2(boolean debug) {
		InputStream in = EmailUtils2.class
				.getResourceAsStream("MailServer.properties");
		try {
			properties.load(in);
			this.mailHost = properties.getProperty("mail.smtp.host");
			this.sender_username = properties
					.getProperty("mail.sender.username");
			this.sender_password = properties
					.getProperty("mail.sender.password");
			
			//设置SSL连接
			properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			properties.setProperty("mail.smtp.port", "465");
			properties.setProperty("mail.smtp.socketFactory.port", "465");
			
		} catch (IOException e) {
		}

		session = Session.getInstance(properties);
		session.setDebug(debug);// 开启后有调试信息
		message = new MimeMessage(session);
	}

	public boolean doSendHtmlEmail(String subject, String sendHtml,
			String receiveUser, File attachment) {
		
		boolean sendstate = true;   //请求发送状态
		try {
			// 发件人
			InternetAddress from = new InternetAddress(sender_username);
			message.setFrom(from);
			// 收件人
			InternetAddress to = new InternetAddress(receiveUser);
			message.setRecipient(Message.RecipientType.TO, to);
			// 邮件主题
			message.setSubject(subject);
			// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
			Multipart multipart = new MimeMultipart();
			// 添加邮件正文
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
			multipart.addBodyPart(contentPart);
			// 添加附件的内容
			if (attachment != null) {
				BodyPart attachmentBodyPart = new MimeBodyPart();
				DataSource source = new FileDataSource(attachment);
				attachmentBodyPart.setDataHandler(new DataHandler(source));
				// MimeUtility.encodeWord可以避免文件名乱码
				attachmentBodyPart.setFileName(MimeUtility
						.encodeWord(attachment.getName()));
				multipart.addBodyPart(attachmentBodyPart);
			}

		
			// 将multipart对象放到message中
			message.setContent(multipart);
			// 保存邮件
			message.saveChanges();
			transport = session.getTransport("smtp");
			// smtp验证,就是你用来发邮件的邮箱用户名密码
			transport.connect(mailHost, sender_username, sender_password);
			// 发送
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			sendstate = false;
		} finally {
			if (transport != null) {
				try {
					transport.close();
				} catch (MessagingException e) {
					sendstate = false;
				}
			}
		}
		return sendstate;
	}

	

	
	
	public static void main(String[] args) {
		String html = "<html><body><h1>哈哈哈</h1><h2>哈哈哈</h2>" +
				"<h3>哈哈哈</h3><h4>哈哈哈</h4>" +
				"<h5>哈哈哈</h5><h6>哈哈哈</h6>" +
				"</body>" + 
				"</html>";
		File file = new File("//192.168.0.14/movies/审计记录.xls");
		System.out.println(EmailUtils2.send("xx科技邮件", html, "12345678@qq.com", file));
	}
}
</pre><pre name="code" class="java">

MailServer.properties



#author DingJiaCheng

#smtp server address
mail.smtp.host=smtp.126.com
mail.smtp.auth=true
mail.sender.username=12345678@126.com
mail.sender.password=************
MailServer.properties


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值