java mail

来源:互联网 发布:上海房产中介排名 知乎 编辑:程序博客网 时间:2024/06/10 12:43

需要activation.jar    mail.jar 包

例子:

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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 javax.mail.internet.MimeUtility;

public class EmailData {

 String from = null; // 发件人

 String[] recipients = null; // 收件人,可以多个

 String subject = null; // 邮件主题

 String content = null; // 邮件内容

 String contentType = null; // 邮件内容格式(text或html)

 String fileName = null; // 附件文件名全路经(目前只提供一个附件)

 public void postMail(EmailData emailData) throws MessagingException,
   Exception {

  String from = emailData.getFrom();
  String[] recipients = emailData.getRecipients();
  String subject = emailData.getSubject();
  String content = emailData.getContent();
  String contentType = emailData.getContentType();
  String fileName = emailData.getFileName();

  if (recipients != null && recipients.length > 0) {

   Properties props = new Properties();
   // 设置邮件服务器地址,连接超时时限等信息
   props.put("mail.smtp.host", "192.168.0.253"); // 10.30.1.233邮件服务器
   props.put("mail.smtp.connectiontimeout", "10000"); //
   props.put("mail.smtp.timeout", "10000"); //

   // 创建缺省的session对象
   Session session = Session.getDefaultInstance(props, null);

   // 创建message对象
   Message msg = new MimeMessage(session);

   // 设置发件人和收件人
   InternetAddress addressFrom = new InternetAddress(from);
   msg.setFrom(addressFrom);
   InternetAddress[] addressTo = new InternetAddress[recipients.length];
   for (int i = 0; i < recipients.length; i++) {
    addressTo[i] = new InternetAddress(recipients[i]);
   }
   msg.setRecipients(Message.RecipientType.TO, addressTo);

   // 设置邮件标题,中文编码
   subject = MimeUtility.encodeText(new String(subject.getBytes(),
     "GB2312"), "GB2312", "B");
   msg.setSubject(subject);

   // 设置邮件内容,区分文本格式和HTML格式
   if (contentType == null || contentType.equals("text")) {
    msg.setText(content);
   } else if (contentType.equals("html")) {
    // 给消息对象设置内容
    BodyPart bodyPart1 = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
    bodyPart1.setContent(content, "text/html;charset=gb2312");// 给BodyPart对象设置内容和格式/编码方式
    Multipart multipart = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
    // 设置邮件附件
    BodyPart bodyPart2 = new MimeBodyPart();
    FileDataSource fileDataSource = new FileDataSource(fileName);
    bodyPart2.setDataHandler(new DataHandler(fileDataSource));
    bodyPart2.setFileName(fileDataSource.getName());

    // 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);

    msg.setContent(multipart);// 把multipart作为消息对象的内容
   }

   // 设置邮件发送时间
   msg.setSentDate(new java.util.Date());
   // 调用抽象类的静态方法send()发送邮件
   Transport.send(msg);
  }
 }

 public static void main(String[] args) {
  
  EmailData emailData = new EmailData();
  
  String[] str = new String[1];
  str[0] = "yuki.wei.zhang@accenture.com";
  
  emailData.setFrom("jie.zhang@eland-g.com");
  emailData.setRecipients(str);
  emailData.setSubject("test");
  emailData.setContent("hello world");
  emailData.setContentType("html");
  emailData.setFileName("c:/EmailData.java");
  
  try {
   emailData.postMail(emailData);
  } catch (MessagingException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 /**
  * @return 返回 content。
  */
 public String getContent() {
  return content;
 }
 /**
  * @param content
  *            要设置的 content。
  */
 public void setContent(String content) {
  this.content = content;
 }
 /**
  * @return 返回 contentType。
  */
 public String getContentType() {
  return contentType;
 }
 /**
  * @param contentType
  *            要设置的 contentType。
  */
 public void setContentType(String contentType) {
  this.contentType = contentType;
 }
 /**
  * @return 返回 fileName。
  */
 public String getFileName() {
  return fileName;
 }
 /**
  * @param fileName 要设置的 fileName。
  */
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
 /**
  * @return 返回 from。
  */
 public String getFrom() {
  return from;
 }
 /**
  * @param from 要设置的 from。
  */
 public void setFrom(String from) {
  this.from = from;
 }
 /**
  * @return 返回 recipients。
  */
 public String[] getRecipients() {
  return recipients;
 }
 /**
  * @param recipients 要设置的 recipients。
  */
 public void setRecipients(String[] recipients) {
  this.recipients = recipients;
 }
 /**
  * @return 返回 subject。
  */
 public String getSubject() {
  return subject;
 }
 /**
  * @param subject 要设置的 subject。
  */
 public void setSubject(String subject) {
  this.subject = subject;
 }

}

 

 

原创粉丝点击