java mail发送邮件

一 相关的类

MailAuthenticator.java类


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * 服务器邮箱登录验证
 * */
public class MailAuthenticator extends Authenticator{
	private String userName = null;//用于发送邮件的邮箱

	private String password = null;//邮箱密码

	public MailAuthenticator() {
    }

	public MailAuthenticator(String username, String password) {
		this.userName = username;
		this.password = password; 
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, password);
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

MailSender.java

import java.util.Date;
import java.util.List;
import java.util.Properties;

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 javax.mail.internet.MimeMessage.RecipientType;

/**
 * 邮件发送器  单独发或者群发
 * */
public class MailSender {

	//发送邮件的props文件
	private final transient Properties props = System.getProperties();

	private transient MailAuthenticator authenticator;//邮件服务器登录验证  

	private transient Session session;//邮箱session

	/**  
	 * 初始化邮件发送器  
	 *  
	 * @param smtpHostName  
	 * SMTP邮件服务器地址  
	 * @param username  
	 * 发送邮件的用户名(地址)  
	 * @param password  
	 * 发送邮件的密码  
	 */ 
	public MailSender(final String smtpHostName, final String username,  
			final String password) {  
		init(username, password, smtpHostName);  
	} 

	/**  
	 * 初始化邮件发送器  
	 *  
	 * @param username  
	 * 发送邮件的用户名(地址),并以此解析SMTP服务器地址  
	 * @param password  
	 * 发送邮件的密码  
	 */ 
	public MailSender(final String username, final String password) {  
		//通过邮箱地址解析出smtp服务器,适合大多数邮箱 
		final String smtpHostName = "smtp." + username.split("@")[1];  
		init(username, password, smtpHostName);  
	}  
	/**  
	 * 初始化  
	 *  
	 * @param username  
	 * 发送邮件的用户名(地址)  
	 * @param password  
	 * 密码  
	 * @param smtpHostName  
	 * SMTP主机地址  
	 */ 
	private void init(String username, String password, String smtpHostName) {  
		// 初始化props  
		props.put("mail.smtp.auth", "true");  
		props.put("mail.smtp.host", smtpHostName);  
		// 验证  
		authenticator = new MailAuthenticator(username, password);  
		// 创建session  
		session = Session.getInstance(props, authenticator);  
	}

	/**  
	 * 发送邮件  
	 *  
	 * @param recipient  
	 * 收件人邮箱地址  
	 * @param subject  
	 * 邮件主题  
	 * @param content  
	 * 邮件内容  
	 * @throws AddressException  
	 * @throws MessagingException  
	 */ 
	public void send(String recipient, String subject, Object content)  
			throws AddressException, MessagingException {  
		// 创建mime类型邮件  
		final MimeMessage message = new MimeMessage(session);  
		// 设置发信人  
		message.setFrom(new InternetAddress(authenticator.getUserName()));  
		// 设置收件人  
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));  
		// 设置主题  
		message.setSubject(subject);
		
		message.setSentDate(new Date());
		
		// 设置邮件内容  
		message.setContent(content.toString(), "text/html;charset=utf-8");  

		Transport.send(message);  
	}

	/**  
	 * 群发邮件  
	 *  
	 * @param recipients  
	 * 收件人们  
	 * @param subject  
	 * 主题  
	 * @param content  
	 * 内容  
	 * @throws AddressException  
	 * @throws MessagingException  
	 */ 
	public void send(List<String> recipients, String subject, Object content)  
			throws AddressException, MessagingException {  
		// 创建mime类型邮件  
		final MimeMessage message = new MimeMessage(session);  
		// 设置发信人  
		message.setFrom(new InternetAddress(authenticator.getUserName()));  
		// 设置收件人们  
		final int num = recipients.size();  
		InternetAddress[] addresses = new InternetAddress[num];  
		for (int i = 0; i <num; i++) {  
			addresses[i] = new InternetAddress(recipients.get(i));  
		}  
		message.setRecipients(RecipientType.TO, addresses);  
		// 设置主题  
		message.setSubject(subject);
		
		message.setSentDate(new Date());
		// 设置邮件内容  
		message.setContent(content.toString(), "text/html;charset=utf-8");  
		// 发送  
		Transport.send(message);  
	}  
	/**  
	 * 发送邮件  
	 *  
	 * @param recipient  
	 * 收件人邮箱地址  
	 * @param mail  
	 * 邮件对象  
	 * @throws AddressException  
	 * @throws MessagingException  
	 */ 
	public void send(String recipient, MailSenderInfo mail)  
			throws AddressException, MessagingException {  
		send(recipient, mail.getSubject(), mail.getContent());  
	}  
	/**  
	 * 群发邮件  
	 *  
	 * @param recipients  
	 * 收件人们  
	 * @param mail  
	 * 邮件对  
	 * @throws AddressException  
	 * @throws MessagingException  
	 */ 
	public void send(List<String> recipients, MailSenderInfo mail)  
			throws AddressException, MessagingException {  
		send(recipients, mail.getSubject(), mail.getContent());  
	}  

}

MailSenderInfo.java


import java.util.Properties;
/**
 * 邮件pojo
 * */
public class MailSenderInfo {
	
	 private String mailServerHost;
	
	 private String mailServerPort = "25";
	
	 private String fromAddress;
	
	 private String toAddress;
	
	 private String userName;
	
	 private String password;
	
	 private boolean validate = false;
	
	 private String subject;
	
	 private String content;
	
	 private String[] attachFileNames;
	 
	 public Properties getProperties() {
	  Properties p = new Properties();
	  p.put("mail.smtp.host", this.mailServerHost);
	  p.put("mail.smtp.port", this.mailServerPort);
	  p.put("mail.smtp.auth", validate ? "true" : "false");
	  return p;
	 }
	 
	 public String getMailServerHost() {
	  return mailServerHost;
	 }
	 public void setMailServerHost(String mailServerHost) {
	  this.mailServerHost = mailServerHost;
	 }
	 public String getMailServerPort() {
	  return mailServerPort;
	 }
	 public void setMailServerPort(String mailServerPort) {
	  this.mailServerPort = mailServerPort;
	 }
	 public boolean isValidate() {
	  return validate;
	 }
	 public void setValidate(boolean validate) {
	  this.validate = validate;
	 }
	 public String[] getAttachFileNames() {
	  return attachFileNames;
	 }
	 public void setAttachFileNames(String[] fileNames) {
	  this.attachFileNames = fileNames;
	 }
	 public String getFromAddress() {
	  return fromAddress;
	 }
	 public void setFromAddress(String fromAddress) {
	  this.fromAddress = fromAddress;
	 }
	 public String getPassword() {
	  return password;
	 }
	 public void setPassword(String password) {
	  this.password = password;
	 }
	 public String getToAddress() {
	  return toAddress;
	 }
	 public void setToAddress(String toAddress) {
	  this.toAddress = toAddress;
	 }
	 public String getUserName() {
	  return userName;
	 }
	 public void setUserName(String userName) {
	  this.userName = userName;
	 }
	 public String getSubject() {
	  return subject;
	 }
	 public void setSubject(String subject) {
	  this.subject = subject;
	 }
	 public String getContent() {
	  return content;
	 }
	 public void setContent(String textContent) {
	  this.content = textContent;
	 }
}

二 测试

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.junit.Test;

import com.nufront.euht.common.email.MailSender;

public class TestMailSender {
	 @Test
	 public void testMailSender() {
		 String smtpHostName = "mail.163.com";
		 String username = "[email protected]";
		 String password = "*****";
		 MailSender mailSender = new MailSender(smtpHostName,username,password);
		 String recipient = "[email protected]";
		 String subject = "邮件主题测试";
		 String content = "邮件内容测试";
		 
		 try {
			mailSender.send(recipient, subject, content);
			System.out.println("成功");
		} catch (Exception e){
			System.out.println("失败");
			e.printStackTrace();
		}
	 }
}



你可能感兴趣的:(java mail发送邮件)