Java mail 发送邮件

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);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值