JAVA 邮件发送

                      邮件发送

 

1.写一个 MailSendProper 类封装需要的属性

import java.util.Properties;

public class MailSendProper {
    private String Host ;          //发送邮件服务器的ip
    private String Port ;          //发送邮件服务器的端口号
    private String SendAddress ;      //邮件发送者的地址
    private String ReceiveAddress ;     //邮件接受者的地址
    private String username ;        //登录发送邮箱的用户名
    private String password ;        //登录发送邮箱的密码
    private boolean isvalidate = true ;   //是否需要身份验证
    private String subject ;          //邮件标题
    private String content ;        //邮件内容

    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;
    }
    public String getHost() {
      return Host;
    }
    public void setHost(String host) {
      Host = host;
    }
    public String getPort() {
      return Port;
    }
    public void setPort(String port) {
      Port = port;
    }
    public String getSendAddress() {
      return SendAddress;
    }
    public void setSendAddress(String sendAddress) {
      SendAddress = sendAddress;
    }
    public String getReceiveAddress() {
      return ReceiveAddress;
    }
    public void setReceiveAddress(String receiveAddress) {
      ReceiveAddress = receiveAddress;
    }
    public String getUsername() {
      return username;
    }
    public void setUsername(String username) {
      this.username = username;
    }
    public String getPassword() {
      return password;
    }
    public void setPassword(String password) {
      this.password = password;  
    }
    public boolean isIsvalidate() {
      return isvalidate;
    }
    public void setIsvalidate(boolean isvalidate) {
      this.isvalidate = isvalidate;
    }
    public Properties getProperties(){
      Properties properties = new Properties() ;
      properties.put("mail.smtp.host", this.Host) ;
      properties.put("mail.smtp.port", this.Port) ;
      properties.put("mail.smtp.auth", isvalidate?"true":"false") ;
      return properties ;
    }
}


 

 

2、写一个  EmailMessage 封装 发送信息

public class EmailMessage {
    private String title ;
    private String context ;
    private String toEmail ;

    public EmailMessage() {
      super();
    }
    public EmailMessage(String title, String context, String toEmail) {
      super();
      this.title = title;
      this.context = context;
      this.toEmail = toEmail;
    }
    public String getTitle() {
      return title;
    }
    public void setTitle(String title) {
      this.title = title;
    }
    public String getContext() {
      return context;
    }
    public void setContext(String context) {
      this.context = context;
    }
    public String getToEmail() {
      return toEmail;
    }
    public void setToEmail(String toEmail) {
      this.toEmail = toEmail;
    }
}


 

3、写一个 MailAttorney  邮箱密码验证器 类

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MailAttorney extends Authenticator {
  private String username ;
  private String password ;

  public MailAttorney(String username,String password) {
    this.username = username ;
    this.password = password ;
  }
  //覆盖父类方法,获取密码认证器
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username,password) ;
  }
}


 

4 、 写一个 MailSend  邮件工具类

import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.VTBBS.entity.EmailMessage;

public class MailSend {
    public static boolean mailTest(MailSendProper mailsender){
        MailAttorney attorney = null ;
        if(mailsender.isIsvalidate()){//判断是否需要身份认证
          attorney = new MailAttorney(mailsender.getUsername(),mailsender.getPassword()) ;
        }
        //根据邮箱会话属性和密码验证器构造一个发送邮箱的seesion
        Session session = Session.getInstance(mailsender.getProperties(),attorney) ;
        //根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(session) ;
        try {
          //创建邮件发送者的地址
          Address from = new InternetAddress(mailsender.getSendAddress()) ;
          //设置邮箱消息的发送者
          mailMessage.setFrom(from);
          //创建邮件接收这的地址,并设置到邮件消息中
          Address to = new InternetAddress(mailsender.getReceiveAddress()) ;
          mailMessage.setRecipient(Message.RecipientType.TO, to);
          mailMessage.setSubject(mailsender.getSubject()); //设置邮件标题
          mailMessage.setSentDate(new Date()); //设置发送邮件时间
          mailMessage.setText(mailsender.getContent()); //设置邮件内容
          Transport.send(mailMessage);
          return true ;
        } catch (Exception e) {
          e.printStackTrace();
          return false ;
        }
    }

    public static boolean sendEmail(EmailMessage message){
        MailSendProper mail = new MailSendProper() ;
        mail.setHost("smtp.126.com");          //smtp简单的邮件传输协议,默认端口号是25,
        mail.setPort("25");
        mail.setIsvalidate(true);              //需呀身份验证
        mail.setUsername("");               //设置登录用户名 
        mail.setPassword("");               //设置发送者的密码
        mail.setSendAddress("");             //设置发送这的地址  发送者地址和登录用户名是同一个
        mail.setReceiveAddress(message.getToEmail());  //设置接收者的地址
        mail.setSubject(message.getTitle());        //设置邮箱标题
        mail.setContent(message.getContext());      //设置邮箱的内容
        return mailTest(mail) ;
    }

}

注意:使用的邮箱必须开启POP3/SMTP服务 才能发送成功,

   不同的邮箱,邮箱的传输协议不一样如

        QQ邮箱:SMTP传输协议是 smtp.qq.com 端口 25

              POP3传输协议是 pop3.qq.com  端口 110


5、使用方法测试

public static void main(String[] args) {
  EmailMessage message = new EmailMessage() ;
  String code = String.valueOf(Math.random()).substring(3, 9) ; //生成验证码
  message.setTitle("邮箱验证");                  //邮件标题
  message.setContext("尊敬的用户你好,你的验证码为"+code+"。"); //邮件内容
  message.setToEmail("940202884@qq.com"); //要发送给谁
  System.out.println(MailSend.sendEmail(message)?"发送成功":"发送失败") ;
}

若要转载,请标明此处

posted @ 2016-09-30 10:56  常风  阅读(384)  评论(2编辑  收藏  举报