关闭

Java发送Email

标签: javaemailMimeMessage
206人阅读 评论(0) 收藏 举报
分类:
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();
		}
	}
	
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:151667次
    • 积分:1376
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:2篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论