`

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  
好!

相关推荐

    java mail发邮件

    在你提供的压缩包文件中,包含了三个关键元素:activation.jar、mail.jar以及SendMail.java,这些都是Java Mail发邮件所需的基本组件。 `activation.jar` 是JavaBeans Activation Framework (JAF) 的一部分,它是...

    java Mail 发送邮件及邮件附件

    Java Mail 发送邮件及邮件附件 Java Mail 是一个广泛使用的 Java 库,用于发送电子邮件。以下是 Java Mail 发送邮件及邮件附件的知识点。 Java Mail 简介 Java Mail 是一个 Java 库,提供了发送电子邮件的功能。...

    Java Mail 发送邮件

    ### Java Mail 发送邮件知识点详解 #### 一、概述 Java Mail API 是一个用于发送与接收电子邮件的标准 Java 库,支持多种协议如 SMTP (Simple Mail Transfer Protocol) 和 POP3 (Post Office Protocol version 3) ...

    java mail 发送邮件

    Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和管理邮件。在Java应用程序中,我们通常使用JavaMail API来实现邮件的自动化操作,比如发送带有附件的邮件、群发邮件等。下面将...

    java mail 发送邮件代码与架包

    Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和操作邮件。这个API提供了广泛的协议支持,包括SMTP(简单邮件传输协议)用于发送邮件,POP3和IMAP4用于接收邮件。在Java Mail中,...

    Java mail发送邮件

    利用Java mail发送邮件,用到的一些类,一些方法,写的很简陋

    使用 java mail 发送邮件

    要使用Java Mail发送邮件,首先需要添加相应的依赖库到项目中。对于Android项目,通常会通过Gradle来引入。 1. 添加依赖: 在项目的build.gradle文件中,添加以下依赖: ```groovy dependencies { ...

    Java Mail发送邮件脚本及所需全部Jar包

    下面我们将详细探讨Java Mail发送邮件脚本的核心知识点,以及在实际操作中可能需要的Jar包。 1. **JavaMail API**:JavaMail API是Java中处理电子邮件的标准库,由JavaMail项目维护。它提供了一系列接口和类,包括`...

    java mail发送邮件

    JavaMail 是一个开源的 Java 库,用于处理电子邮件的发送、接收和其他相关任务。它提供了对各种邮件协议(如 SMTP、POP3 和 IMAP)的支持,并且可以与 MIME 格式的邮件内容配合使用,允许发送包含附件、HTML 内容等...

    java mail 发邮件 demo程序

    这个“java mail 发邮件 demo程序”是为了演示如何使用JavaMail API发送电子邮件而创建的一个简单示例。在这个DEMO程序中,我们将深入探讨JavaMail API的核心概念以及如何构建一个实际的邮件发送功能。 首先,...

    Java mail发送邮件(源代码带注释)

    这个标题“Java mail发送邮件(源代码带注释)”表明我们将讨论如何使用JavaMail API来实现电子邮件的发送功能。描述提到,通过简单的赋值和测试,我们可以快速实现邮件发送,这表明提供的源代码应该是一个易于理解且...

    用java mail发邮件源码,支持多种邮局,支持单发和群发

    Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和管理邮件。这个源码可能包含了一套完整的解决方案,可以与多种邮局(如Gmail, Yahoo, Hotmail等)进行交互,并且支持单个收件人和...

    java mail发送邮件(源代码+注释)

    Java Mail 是一个强大的开源API,用于处理电子邮件的发送和接收。这个API允许开发者在Java应用程序中集成邮件功能,包括SMTP(简单邮件传输协议)和POP3(邮局协议第三版)等邮件服务协议。本教程将详细介绍如何使用...

    java mail 邮件发送

    以下是一个简单的Java Mail发送邮件的示例代码: ```java import javax.mail.*; import javax.mail.internet.*; public class EmailSender { public static void main(String[] args) throws Exception { // ...

    java mail 发送QQ邮件

    要使用Java Mail发送QQ邮件,首先需要在QQ邮箱设置中开启“POP3/SMTP/IMAP”服务,并获取授权码。授权码是QQ邮箱为第三方应用提供的一种安全验证方式,不同于普通密码。 ### 3. 添加依赖 在Java项目中,我们需要...

    Java Mail邮件发送源码下载

    Java Mail邮件发送是Java开发中常见的一项功能,用于在应用程序中发送电子邮件。Java Mail API提供了一个标准的接口,使得开发者可以方便地实现SMTP(简单邮件传输协议)和其他邮件协议的功能。下面将详细介绍Java ...

    Java Mail demo (仅发送邮件)

    下面是一个简化的Java Mail发送邮件的步骤,展示了一些关键API的使用: 1. 引入Java Mail依赖:首先确保项目中包含了Java Mail的相关库,通常通过Maven或Gradle等构建工具添加依赖。 2. 创建Properties对象:设置...

Global site tag (gtag.js) - Google Analytics