欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java邮件发送的实现

程序员文章站 2024-03-12 12:31:02
邮件是在日常生活中,这篇文章主要介绍了java实现基于smtp发送邮件的方法,实例分析了java基于smtp服务发送邮件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考...

邮件是在日常生活中,这篇文章主要介绍了java实现基于smtp发送邮件的方法,实例分析了java基于smtp服务发送邮件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下.

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)?"发送成功":"发送失败") ;
}

希望本文所述对大家学习java程序设计有所帮助。