Java发送Email

原创 2015年11月19日 14:48:19
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendEmailUtil {
	
	//发送单个邮箱
	public void sendSingleMail(String emailAddress, String content, String title) {
		String [] emailAddresses = {emailAddress};
		sendGroupMail(emailAddresses, content, title);
	}
	
	// 发送多个邮箱
	public void sendGroupMail(String[] emailAddresses, String content, String title) {
		String emailAddressFrom = "123456@qq.com";//邮箱地址
		String emailServeiHost = "123456@qq.com";//邮箱地址
		final String username = "test";//用户名
		final String password ="test";//密码
		String nickname = "测试账号";//发件人昵称
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", emailServeiHost);
			props.put("mail.smtp.auth", "true");
			Session session = Session.getDefaultInstance(props,
					new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username,password);
				}
			});
			
			MimeMessage mimeMessage = new MimeMessage(session);
			
			mimeMessage.setFrom(new InternetAddress(MimeUtility.encodeText(nickname)+" <"+emailAddressFrom+">"));
			InternetAddress[] emailAddressTo = new InternetAddress[emailAddresses.length];
			for (int i = 0; i < emailAddresses.length; i++) {
				emailAddressTo[i] = new InternetAddress(emailAddresses[i]);
			}
			
			mimeMessage.setSubject(title);
			mimeMessage.setRecipients(Message.RecipientType.TO, emailAddressTo);
			Multipart multipart = new MimeMultipart();
			MimeBodyPart mailContent = new MimeBodyPart();
			mailContent.setContent(content, "text/html;charset=utf-8");
			multipart.addBodyPart(mailContent);
			mimeMessage.setContent(multipart);
			mimeMessage.setSentDate(new Date());
			Transport.send(mimeMessage);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

java 发送 Email

【java】实现发送带附件email

一、目标 用java代码实现发送带附件email的功能 二、学习地址 http://wenku.baidu.com/view/0b5acc82e53a580216fcfe4e.html h...

java发送email

  • 2011-11-27 17:11
  • 378KB
  • 下载

java发送email

package com.zhou.email; import java.io.IOException; import java.io.InputStream; import java.uti...

java发送Email邮件

JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件。这一机制被广泛的用在注...

java 发送Email 需要的包

  • 2013-04-21 22:00
  • 386KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)