疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java实现邮件发送功能


 

   1、第一个文件 MailSenderInfo.java

  点击(此处)折叠或打开

  /**

  * 发送邮件需要使用的基本信息

  */

  import java.util.Properties;

  public class MailSenderInfo {

  // 发送邮件的服务器的IP和端口

  private String mailServerHost;

  private String mailServerPort = "25";

  // 邮件发送者的地址

  private String fromAddress;

  // 邮件接收者的地址

  private String toAddress;

  // 登陆邮件发送服务器的用户名和密码

  private String userName;

  private String password;

  // 是否需要身份验证

  private boolean validate = false;

  // 邮件主题

  private String subject;

  // 邮件的文本内容

  private String content;

  // 邮件附件的文件名

  private String[] attachFileNames;

  /**

  * 获得邮件会话属性

  */

  public Properties getProperties(){

  Properties p = new Properties();

  p.put("mail.smtp.host", this.mailServerHost);

  p.put("mail.smtp.port", this.mailServerPort);

  p.put("mail.smtp.auth", validate ? "true" : "false");

  return p;

  }

  public String getMailServerHost() {

  return mailServerHost;

  }

  public void setMailServerHost(String mailServerHost) {

  this.mailServerHost = mailServerHost;

  }

  public String getMailServerPort() {

  return mailServerPort;

  }

  public void setMailServerPort(String mailServerPort) {

  this.mailServerPort = mailServerPort;

  }

  public boolean isValidate() {

  return validate;

  }

  public void setValidate(boolean validate) {

  this.validate = validate;

  }

  public String[] getAttachFileNames() {

  return attachFileNames;

  }

  public void setAttachFileNames(String[] fileNames) {

  this.attachFileNames = fileNames;

  }

  public String getFromAddress() {

  return fromAddress;

  }

  public void setFromAddress(String fromAddress) {

  this.fromAddress = fromAddress;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  this.password = password;

  }

  public String getToAddress() {

  return toAddress;

  }

  public void setToAddress(String toAddress) {

  this.toAddress = toAddress;

  }

  public String getUserName() {

  return userName;

  }

  public void setUserName(String userName) {

  this.userName = userName;

  }

  public String getSubject() {

  return subject;

  }

  public void setSubject(String subject) {

  this.subject = subject;

  }

  public String getContent() {

  return content;

  }

  public void setContent(String textContent) {

  this.content = textContent;

  }

  }

  2、第二个文件 MyAuthenticator.java

  点击(此处)折叠或打开

  import javax.mail.*;

  public class MyAuthenticator extends Authenticator{

  String userName=null;

  String password=null;

  public MyAuthenticator(){

  }

  public MyAuthenticator(String username, String password) {

  this.userName = username;

  this.password = password;

  }

  protected PasswordAuthentication getPasswordAuthentication(){

  return new PasswordAuthentication(userName, password);

  }

  }

  3、第三个文件 SimpleMailSender.java

  点击(此处)折叠或打开

  import java.util.Date;

  import java.util.Properties;

  import javax.mail.Address;

  import javax.mail.BodyPart;

  import javax.mail.Message;

  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;

  /**

  * 简单邮件(不带附件的邮件)发送器

  */

  public class SimpleMailSender {

  /**

  * 以文本格式发送邮件

  * @param mailInfo 待发送的邮件的信息

  */

  public void sendTextMail(MailSenderInfo mailInfo) throws Exception{

  MyAuthenticator authenticator = null;

  Properties pro = mailInfo.getProperties();

  if (mailInfo.isValidate()) {

  authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

  }

  Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

  Message mailMessage = new MimeMessage(sendMailSession);

  Address from = new InternetAddress(mailInfo.getFromAddress());

  mailMessage.setFrom(from);

  Address to = new InternetAddress(mailInfo.getToAddress());

  mailMessage.setRecipient(Message.RecipientType.TO,to);

  mailMessage.setSubject(mailInfo.getSubject());

  mailMessage.setSentDate(new Date());

  String mailContent = mailInfo.getContent();

  mailMessage.setText(mailContent);

  Transport.send(mailMessage);

  }

  /**

  * 以HTML格式发送邮件

  * @param mailInfo 待发送的邮件信息

  */

  public static void sendHtmlMail(MailSenderInfo mailInfo) throws Exception {

  MyAuthenticator authenticator = null;

  Properties pro = mailInfo.getProperties();

  if (mailInfo.isValidate()) {

  authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

  }

  Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

  Message mailMessage = new MimeMessage(sendMailSession);

  Address from = new InternetAddress(mailInfo.getFromAddress());

  mailMessage.setFrom(from);

  Address to = new InternetAddress(mailInfo.getToAddress());

  mailMessage.setRecipient(Message.RecipientType.TO,to);

  mailMessage.setSubject(mailInfo.getSubject());

  mailMessage.setSentDate(new Date());

  Multipart mainPart = new MimeMultipart();

  BodyPart html = new MimeBodyPart();

  html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");

  mainPart.addBodyPart(html);

  mailMessage.setContent(mainPart);

  Transport.send(mailMessage);

  }

  }

  4、测试Main Test.java

  点击(此处)折叠或打开

  import org.apache.commons.logging.Log;

  import org.apache.commons.logging.LogFactory;

  public class Test {

  /**

  * 日志输出对象

  */

  private static final Log log = LogFactory.getLog(Test.class);

  /**

  * 测试Main方法

  * @param args

  */

  public static void main(String [] args){

  try {

  sendEmail("test1@126.com","test2@126.com","邮件标题","邮件内容");

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  /**

  * 邮件发送工具方法

  * @param fromAddress

  * @param toAddress

  * @param content

  * @throws Exception

  */

  public static void sendEmail(String fromAddress, String toAddress,String title ,String content) throws Exception{

  log.info("执行邮件发送(发送地址:"+fromAddress+ " 接收地址:" + toAddress +")");

  log.debug("发送邮件标题:" + title + " 邮件内容:" + content);

  SimpleMailSender sms = new SimpleMailSender();

  MailSenderInfo mailInfo = new MailSenderInfo();

  mailInfo.setMailServerHost("smtp.domain.com"); //邮件服务器地址

  mailInfo.setMailServerPort("25"); //邮件服务器端口

  mailInfo.setValidate(true); //是否进行用户名密码验证

  mailInfo.setUserName("username"); //用户名

  mailInfo.setPassword("passwd"); //密码

  mailInfo.setFromAddress(fromAddress); //发送地址

  mailInfo.setToAddress(toAddress); //接收地址

  mailInfo.setSubject(title);

  mailInfo.setContent(content);

  sms.sendHtmlMail(mailInfo);

  }

  }