使用JavaMail 编程来收发邮件

package com.email;

/**
 * 配置邮件的JavaBean
 * @author liky
 *
 */

public class MailConfigBean {
 private String smtpHost;
 private String mailFrom;
 private String transportProtocol;
 private String smtpPort;
 private String userName;
 private String userPwd;
 private String needAuth;

 public String getNeedAuth() {
  return needAuth;
 }

 public void setNeedAuth(String needAuth) {
  this.needAuth = needAuth;
 }

 public String getSmtpHost() {
  return smtpHost;
 }

 public void setSmtpHost(String smtpHost) {
  this.smtpHost = smtpHost;
 }

 public String getSmtpPort() {
  return smtpPort;
 }

 public void setSmtpPort(String smtpPort) {
  this.smtpPort = smtpPort;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getUserPwd() {
  return userPwd;
 }

 public void setUserPwd(String userPwd) {
  this.userPwd = userPwd;
 }

 public String getTransportProtocol() {
  return transportProtocol;
 }

 public void setTransportProtocol(String transportProtocol) {
  this.transportProtocol = transportProtocol;
 }

 public String getMailFrom() {
  return mailFrom;
 }

 public void setMailFrom(String mailFrom) {
  this.mailFrom = mailFrom;
 }

}

/*==========================================================*/

package com.email;

/**
 * 设置邮件内容的JavaBean
 * @author liky
 *
 */

public class MailContentBean {

 private String mailTo; 
 private String subject;
 private String content;

 public String getMailTo() {
  return mailTo;
 }

 public void setMailTo(String mailTo) {
  this.mailTo = mailTo;
 }

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

}

 

 

/*===============================================*/

package com.email;

import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 邮件发送的主类
 *
 * @author liky
 *
 */

public class MailSender {
 private MailConfigBean configBean;
 private MailContentBean contentBean;
 private Properties mailProps;

 private Session session;
 private MimeMessage message;
 private Multipart multipart;

 public MailSender(MailConfigBean configBean, MailContentBean contentBean) {
  this.configBean = configBean;
  this.contentBean = contentBean;
  this.mailProps = System.getProperties();

 }

 private boolean setProperties() {

  if (configBean.getTransportProtocol() != null) {
   mailProps.put("mail.transport.protocol", configBean
     .getTransportProtocol());
   System.out.println("设置邮件协议!");
  } else {
   System.err.println("邮件协议为空!");
   return false;
  }
  if (configBean.getSmtpHost() != null) {
   mailProps.put("mail.smtp.host", configBean.getSmtpHost());
   System.out.println("设置邮件SMTP主机!");
  } else {
   System.err.println("SMTP主机为空!");
   return false;
  }
  if (configBean.getSmtpPort() != null) {
   mailProps.put("mail.smtp.port", configBean.getSmtpPort());
   System.out.println("设置SMTP端口号!");
  } else {
   System.err.println("SMTP端口为空!");
   return false;
  }
  if (configBean.getNeedAuth() != null) {
   mailProps.put("mail.smtp.auth", configBean.getNeedAuth());
   System.out.println("设置邮件是否需要验证!");
  } else {
   System.err.println("邮件是否需要验证为空!");
   return false;
  }
  return true;
 }

 private boolean createMimeMessage() {
  if (!setProperties()) {
   System.err.println("设置邮件属性时出错!");
   return false;
  }
  try {
   session = Session.getDefaultInstance(mailProps, null);
   message = new MimeMessage(session);
   multipart = new MimeMultipart();

   return true;
  } catch (Exception ex) {
   System.err.println("创建邮件时出错!");
   return false;
  }
 }

 private boolean setFrom() {
  if (configBean.getMailFrom() != null) {
   try {
    InternetAddress[] mailTo = InternetAddress.parse(configBean
      .getMailFrom());
    message.setRecipients(Message.RecipientType.TO, mailTo);

    System.out.println("设置发件人的地址!");
    return true;
   } catch (AddressException ae) {
    System.err.println("解析发件人地址时出错!");
    return false;
   } catch (MessagingException me) {
    System.err.println("设置发件人地址时出错!");
    return false;
   }

  } else {
   System.err.println("发件人地址为空!");
   return false;
  }
 }

 private boolean setTo() {
  if (contentBean.getMailTo() != null) {
   try {
    InternetAddress[] mailTo = InternetAddress.parse(contentBean
      .getMailTo());
    message.setRecipients(Message.RecipientType.TO, mailTo);
    System.out.println("设置接收邮件的地址!");
    return true;
   } catch (AddressException ae) {
    System.err.println("解析收件人地址时出错!");
    return false;
   } catch (MessagingException me) {
    System.err.println("设置接收邮件的地址出错!");
    return false;
   }
  } else {
   System.err.println("收件人地址为空!");
   return false;
  }
 }

 public boolean setCopyTo() {
  if (contentBean.getMailCopyTo() != null) {
   try {
    InternetAddress[] mailCopyTo = InternetAddress
      .parse(contentBean.getMailCopyTo());
    message.setRecipients(Message.RecipientType.CC, mailCopyTo);
    System.out.println("设置接收邮件的地址!");
    return true;
   } catch (AddressException ae) {
    System.err.println("解析抄送人地址时出错!");
    return false;
   } catch (MessagingException me) {
    System.err.println("设置抄送人地址出错!");
    return false;
   }
  } else {
   System.err.println("抄送人地址为空!");
   return false;
  }
 }

 private boolean setSubject() {
  if (contentBean.getSubject() != null) {
   try {
    message.setSubject(contentBean.getSubject());
    System.out.println("设置邮件的主题!");
    return true;
   } catch (MessagingException me) {
    System.err.println("设置邮件主题时出错!");
    return false;
   }
  } else {
   System.err.println("邮件主题为空!");
   return false;
  }
 }

 private boolean setAttachment() {
  String file = contentBean.getAttachment();

  if (file == null) {
   System.err.println("附件为空!");
   return false;
  }

  try {
   BodyPart bodyPart = new MimeBodyPart();

   FileDataSource dataSource = new FileDataSource(file);
   DataHandler dataHandler = new DataHandler(dataSource);
   bodyPart.setDataHandler(dataHandler);
   bodyPart.setFileName(dataSource.getName());
   multipart.addBodyPart(bodyPart);

  } catch (MessagingException me) {
   System.err.println("添加附件时出错!");
   return false;
  }
  return true;
 }

 private boolean setBody() {
  if (contentBean.getContent() != null) {
   Object content = "<meta http-equiv=Content-Type content=text/html; charset=GBK>"
     + contentBean.getContent();
   String contentType = "text/html; charset=GBK";

   BodyPart bodyPart = new MimeBodyPart();

   try {
    bodyPart.setContent(content, contentType);
    multipart.addBodyPart(bodyPart);

    System.out.println("设置邮件的内容!");

    return true;
   } catch (MessagingException e) {
    System.err.println("设置邮件内容时出错!");
    return false;
   }
  } else {
   System.err.println("邮的内容为空!");
   return false;
  }
 }

 public boolean setSendDate() {
  try {
   message.setSentDate(new Date());
   return true;
  } catch (MessagingException me) {
   System.err.println("设置邮件发送日期时出错!");
   return false;
  }
 }

 public boolean send() {

  String userName = configBean.getUserName();
  String userPwd = configBean.getUserPwd();
  String smtpHost = configBean.getSmtpHost();

  if (!createMimeMessage()) {
   System.err.println("创建邮件时出错!");
   return false;
  }
  if (!setFrom()) {
   return false;
  }
  if (!setTo()) {
   return false;
  }
  if (!setCopyTo()) {
   System.err.println("没有添加抄送人!");
  }
  if (!setSubject()) {
   return false;
  }
  if (!setAttachment()) {
   System.err.println("没有添加附件!");
  }
  if (!setBody()) {
   return false;
  }
  if (!setSendDate()) {
   return false;
  }

  if (userName != null && userPwd != null && smtpHost != null) {
   try {

    message.setContent(multipart);
    message.saveChanges();

    Transport transport = session.getTransport();
    transport.connect(smtpHost, userName, userPwd);

    Transport.send(message);
    transport.close();    

    return true;
   } catch (MessagingException me) {
    me.printStackTrace();
    return false;
   }
  } else {
   System.err.println("用户名或密码为空!");
  }
  return false;
 }

/*

这里进行了测试,在测试可以下载一个称为CMailServer的邮件服务器来进行

*/

 public static void main(String[] args) { 
  String rs = "邮件没有发送成功!";

  MailConfigBean configBean = new MailConfigBean();
  MailContentBean contentBean = new MailContentBean();

  // 设置邮件的内容
  contentBean.setMailTo("liky@liky.com");
  contentBean.setMailCopyTo("lucky@liky.com,lucy@liky.com");
  contentBean.setSubject("I love you");
  contentBean.setContent("测试邮件");
  contentBean.setAttachment("c://boot.ini");

  // 配置邮件
  configBean.setMailFrom("lucy@liky.com");
  configBean.setSmtpHost("192.168.1.192");
  configBean.setNeedAuth("false");
  configBean.setSmtpPort("25");
  configBean.setTransportProtocol("smtp");
  configBean.setUserName("lucy");
  configBean.setUserPwd("redhat");

  MailSender sender = new MailSender(configBean, contentBean);

  if (sender.send()) {
   rs = "邮件发送成功,请查收!";
  }
  System.out.println(rs);
 }

}

/*

这个示例演示了如何用JavaMail进行编程,来发送和接受邮件,

我在每一步都有相应的注释和异常处理

*/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值