javax.mail学习笔记

最近自己做项目要用到发送邮件,查了一下,发现有个javax.mail.jar的包可以使用,自己的尝试了一下

首先自己写一个EMail的类,包含(主题、文本内容、发送者邮箱、接收者邮箱、附件列表)

import java.io.File;
import java.util.List;
import java.util.Vector;

public class EMail {
	
	private String subject;
	private String content;
	private String fromAddress;
	private String toAddress;
	private List<File> files;
	
	public EMail(){
		files = new Vector<File>();
	}
	/**
	 * 添加附件
	 * @param file
	 */
	public void addFile(File file){
		files.add(file);
	}
	
	public String getSubject() {
		return subject;
	}
	
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	public String getContent() {
		return content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public String getFromAddress() {
		return fromAddress;
	}
	
	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}
	
	public String getToAddress() {
		return toAddress;
	}
	
	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}
	
	public List<File> getFiles() {
		return files;
	}
	
}


然后写一个邮件发送器


import java.util.List;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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 MailSender {

	private Session session;
	private Message msg;
	private MimeMultipart mailContent;
	private final String MAIL_ACCOUNT = "********@163.com";//邮箱账号
	private final String MAIL_PASSWORD = "********";//邮箱密码
	
	private static MailSender instance;

	private MailSender() {
		Properties properties = new Properties();
		// 是否需要认证
		properties.setProperty("mail.smtp.auth", "true"); 
		// 设置服务器,如果这里设置163的服务器,就必须用163邮箱发送邮件 (下面MyAuthentication中设置的邮箱账号跟密码)
		properties.setProperty("mail.smtp.host", "smtp.163.com"); 
		// 邮件传输协议(smtp、pop3)
		properties.setProperty("mail.transport.protocol", "smtp"); 
		session = Session.getDefaultInstance(properties, new MyAuthentication());
		session.setDebug(true); //显示发送报告
	}

	/**
	 * 使用单例模式使整个程序只有一个邮件发送器
	 * @return
	 */
	public static MailSender getInstance() {
		if (instance == null) {
			instance = new MailSender();
		}
		return instance;
	}
	
	/**
	 * 发送邮件
	 * @param mail
	 */
	public void sendMail(EMail mail) {
		msg = new MimeMessage(session);  
		mailContent = new MimeMultipart();
		try {
			//设置发件人地址
			msg.setFrom(new InternetAddress(MAIL_ACCOUNT));
			/***
			 	第一个参数为发送方式
			 		RecipientType.TO  收件人 
			 		RecipientType.CC  抄送 
			 		RecipientType.BCC   暗送
			 	第二个参数为接收人邮箱地址,支持多个接收者
			 		setRecipient(RecipientType type, Address address) throws MessagingException
			 		setRecipient(RecipientType type, Address[] address) throws MessagingException
			***/
			msg.setRecipient(RecipientType.TO, new InternetAddress(mail.getToAddress())); 
			//添加邮件内容
			addFile(mail.getFiles());
			setContent(mail.getContent()== null ? "" : mail.getContent());
			setSubject(mail.getSubject() == null ? "" : mail.getSubject());
	        msg.setContent(mailContent);
	        //发送邮件
			Transport.send(msg);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 设置认证邮箱跟密码
	 * @author Administrator
	 */
	private class MyAuthentication extends Authenticator{
		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			// TODO Auto-generated method stub
			return new PasswordAuthentication( MAIL_ACCOUNT , MAIL_PASSWORD );
		}
	}
	
	/**
	 * 添加附件
	 * @param file
	 */
	public void addFile(List<File> files){
		BodyPart fileBodyPart = null;
		FileDataSource fds = null;
		for (File file : files) {
			try {
				fileBodyPart = new MimeBodyPart();
				//获得附件
				fds = new FileDataSource(file.getAbsolutePath()); 
				//将附件加入BodyPart
				fileBodyPart.setDataHandler(new DataHandler(fds)); 
				// 设置附件名称
				fileBodyPart.setFileName(MimeUtility.encodeText(file.getName()));
				mailContent.addBodyPart(fileBodyPart);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (MessagingException e) {
				e.printStackTrace();
			} 
		}
	}
	
	/**
	 * 设置邮件的文本内容
	 * @param content
	 */
	public void setContent(String content){
		//创建文本内容
        BodyPart textBodyPart = new MimeBodyPart();
        try {
			textBodyPart.setContent(content, "text/html;charset=gb2312");
			mailContent.addBodyPart(textBodyPart);
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 设置邮件主题
	 * @param subject
	 */
	public void setSubject(String subject){
		try {
			msg.setSubject(subject);
		} catch (MessagingException e) {
			e.printStackTrace();
		}  
	}
	
	
}

最后只要实例化一个EMail对象,然后设置主题,内容,接收者

然后调用MailSender的send方法,把邮件对象传进去就可以啦



你可能感兴趣的:(android,邮件,javax.mail)