`

Java mail 发送邮件

    博客分类:
  • j2ee
阅读更多

pojo

    pojo.Email.java

package pojo;

import java.util.ArrayList;
import java.util.List;

public class Email {
	
	private String sender;
	private String senderName;
	private List<String> receivers = new ArrayList<String>();
	private List<String> bccList = new ArrayList<String>();
	private List<String> ccList= new ArrayList<String>();
	private String subject;
	private String body;
	
	
	private List<Attachment> attachments = new ArrayList<Attachment>();

	

	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getSenderName() {
		return senderName;
	}

	public void setSenderName(String senderName) {
		this.senderName = senderName;
	}

	public List<String> getReceivers() {
		return receivers;
	}

	public void setReceivers(List<String> receivers) {
		this.receivers = receivers;
	}

	public List<String> getBccList() {
		return bccList;
	}

	public void setBccList(List<String> bccList) {
		this.bccList = bccList;
	}

	public List<String> getCcList() {
		return ccList;
	}

	public void setCcList(List<String> ccList) {
		this.ccList = ccList;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

}

 

    pojo.Attachment.java

package pojo;

public class Attachment {
	private String attachmentName;
	private String attachmentLocation;

	public Attachment() {
	}

	public Attachment(String attachmentName, String attachmentLocation) {
		this.attachmentName = attachmentName;
		this.attachmentLocation = attachmentLocation;
	}

	public String getAttachmentName() {
		return attachmentName;
	}

	public void setAttachmentName(String attachmentName) {
		this.attachmentName = attachmentName;
	}

	public String getAttachmentLocation() {
		return attachmentLocation;
	}

	public void setAttachmentLocation(String attachmentLocation) {
		this.attachmentLocation = attachmentLocation;
	}

}

 

helper

    helper.DefaultAuthenticator.java 

package helper;

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

public class DefaultAuthenticator extends Authenticator {
	private String userName = null;
	private String password = null;

	public DefaultAuthenticator() {
	}

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

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

 

    helper.MailOperator.java

package helper;

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

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
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 pojo.Attachment;
import pojo.Email;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class MailOperator {
	private static MailOperator mailOperator = new MailOperator();
	
	private MailOperator() {
	}

	public static MailOperator getInstance() {
		return mailOperator;
	}
	private Properties getMailProperties() {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.qq.com");
		props.setProperty("mail.smtp.auth", "true");
		return props;
	}
	
	private Message getMailMessage(){
		Properties props = getMailProperties();
		// get the authenticator based on the user name and password
		DefaultAuthenticator authenticator = new DefaultAuthenticator("username", "password");

		// construct the session
		Session sendMailSession = Session.getDefaultInstance(props,authenticator);
		sendMailSession.setDebug(true);
		
		// create the message
		Message mailMessage = new MimeMessage(sendMailSession);
		return mailMessage;
	}
	
	private Multipart getMailContent(String body,List<Attachment> attachments) throws MessagingException, UnsupportedEncodingException{
		// add the text content
		Multipart multipart = new MimeMultipart();
		BodyPart textBodyPart = new MimeBodyPart();
		// textBodyPart.setText(body);
		// set the email type as html
		textBodyPart.setContent(body, "text/html;charset=utf-8");
		multipart.addBodyPart(textBodyPart);
        
		// add the attachment
        for(Attachment attachment : attachments){
        	String attachmentName = attachment.getAttachmentName();
        	String attachmentLocation = attachment.getAttachmentLocation();
            BodyPart attachmentBodyPart= new MimeBodyPart();
            
            // resolve the Chinese characters
			BASE64Encoder enc = new BASE64Encoder();
			attachmentBodyPart.setFileName("=?GBK?B?"+ enc.encode(attachmentName.getBytes()) + "?=");
			DataSource source = new FileDataSource(attachmentLocation);
			attachmentBodyPart.setDataHandler(new DataHandler(source));
        	
        	multipart.addBodyPart(attachmentBodyPart);
        }
		return multipart;
	}

	public void sendTextMail(Email email) throws Exception{
		String sender = email.getSender();
		String senderName = email.getSenderName();
		String subject = email.getSubject();
		String body = email.getBody();
		List<String> receivers = email.getReceivers();
		List<String> ccList = email.getCcList();
		List<String> bccList = email.getBccList();
		List<Attachment> attachments = email.getAttachments();
		
		// get the mail message
		Message mailMessage = getMailMessage();
		
		// set the sender information
		Address from = new InternetAddress(sender,senderName);
		mailMessage.setFrom(from);
		
		// set the receivers informations
		for(String receiver : receivers){
			Address to = new InternetAddress(receiver);
			mailMessage.setRecipient(Message.RecipientType.TO, to);
		}
		
		// set the carbon copy information
		for(String cc : ccList){
			Address to = new InternetAddress(cc);
			mailMessage.setRecipient(Message.RecipientType.CC, to);
		}
		
		// set the blind carbon copy information
		for(String bcc : bccList){
			Address to = new InternetAddress(bcc);
			mailMessage.setRecipient(Message.RecipientType.BCC, to);
		}

		// set the subject information
		mailMessage.setSubject(subject);
		
		// set the send time
		mailMessage.setSentDate(new Date());
		
		// set the email body information
		Multipart multipart = getMailContent(body, attachments);
		mailMessage.setContent(multipart);
		
		// send the information
		Transport.send(mailMessage);
	}
}

 

测试类 test.Main.java

package test;

import helper.MailOperator;
import pojo.Attachment;
import pojo.Email;

public class Main {

	public static void main(String[] args) throws Exception{
		Email email = new Email();
		email.setSender("sender email address");
		email.setSubject("email subject");
		email.setBody("<br/><font color ='red'> email body</font>");
		email.getReceivers().add("receiver address");
		email.getAttachments().add(new Attachment("attachmentName","attachment location in local server"));
		
		MailOperator.getInstance().sendTextMail(email);
	}

}

 

分享到:
评论
5 楼 lijiejava 2014-07-14  
407 can you understand ?
4 楼 lijiejava 2014-07-14  
朱少一出,谁与争锋!
3 楼 lijiejava 2014-07-13  
晏子使楚。楚人以晏子短,楚人为小门于大门之侧而延晏子。晏子不入,曰:“使狗国者从狗门入。今臣使楚,不当从此门入。”傧者更道,从大门入。见楚王,王曰:“齐无人耶?使子为使。”晏子对曰:“齐之临淄三百闾,张袂成阴,挥汗成雨,比肩继踵而在,何为无人?”王曰:“然则何为使子?”晏子对曰:“齐命使,各有所主。其贤者使使贤主,不肖者使使不肖主。婴最不肖,故宜使楚矣。”
晏子将使楚。楚王闻之,谓左右曰:“晏(yàn)婴,齐之习辞者也。今方来,吾欲辱之,何以也?”左右对曰:“为其来也,臣请缚(fù)一人,过王而行,王曰:‘何为者也?’对曰:‘齐人也。’王曰:‘何坐?’曰:‘坐盗。’晏子至,楚王赐晏子酒,酒酣,吏二缚一人诣(yì)王。王曰:“缚者曷(hé)为者也?”对曰:“齐人也,坐盗。”王视晏子曰:“齐人固善盗乎?”晏子避席对曰:“婴闻之,橘生淮南则为橘,生于淮北则为枳,叶徒相似,其实味不同。所以然者何?水土异也。今民生长于齐不盗,入楚则盗,得无楚之水土使民善盗耶?”王笑曰:“ 圣人非所与熙也,寡人反取病焉。”
2 楼 lijiejava 2014-07-13  
好!
1 楼 lijiejava 2014-07-13  
好!

相关推荐

Global site tag (gtag.js) - Google Analytics