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

java使用Socket实现SMTP协议发送邮件

程序员文章站 2024-03-12 12:34:44
本文实例为大家分享了java 利用socket实现smtp协议发送邮件的具体代码,供大家参考,具体内容如下 package mail; import ja...

本文实例为大家分享了java 利用socket实现smtp协议发送邮件的具体代码,供大家参考,具体内容如下

package mail;
 
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.printwriter;
import java.io.reader;
import java.net.socket;
import java.util.arraylist;
import java.util.list;
 
import org.apache.commons.codec.binary.base64;
 
public class mail {
 
  public static void main(string[] args) throws ioexception {
    mail mail = new mail();
    mail.setsmtpserver("smtp.qq.com");
    mail.setfrommail("1344364****@qq.com");
    mail.addtomail("105648****@qq.com");
    mail.addtomail("long*****@sina.com");
    mail.setusername("134364****");
    mail.setpassword("*************");
    mail.setsubject("测试邮件");
    mail.setcontent("<h1>你好</h1><br/><img src=\"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif\" />");
    mail.setshowlog(true);
    mail.send();
    system.out.println("程序结束");
  }
 
  /** 邮件主题 **/
  private string subject;
  /** 从此地址发出 **/
  private string frommail;
  /** 用户名 **/
  private string username;
  /** 登录密码 **/
  private string password;
  /** smtp 服务器地址 **/
  private string smtpserver;
  /** smtp 服务器端口(默认:25) **/
  private int smtpport = 25;
  /** 发送到 tomail 中的所有地址 **/
  private list<string> tomail;
  /** 邮件内容 **/
  private string content;
  /** 是否显示日志 **/
  private boolean showlog;
 
  public void addtomail(string mail) {
    if (tomail == null)
      tomail = new arraylist<string>();
    tomail.add(mail);
  }
 
  public void send() {
    if (smtpserver == null) {
      throw new runtimeexception("smtpserver 不能为空");
    }
    if (username == null) {
      throw new runtimeexception("username 不能为空");
    }
    if (password == null) {
      throw new runtimeexception("password 不能为空");
    }
    if (frommail == null) {
      throw new runtimeexception("frommail 不能为空");
    }
    if (tomail == null || tomail.isempty()) {
      throw new runtimeexception("tomail 不能为空");
    }
    if (content == null || tomail.isempty()) {
      throw new runtimeexception("content 不能为空");
    }
 
    socket socket = null;
    inputstream in = null;
    outputstream out = null;
    try {
      socket = new socket(smtpserver, smtpport);
      socket.setsotimeout(3000);
      in = socket.getinputstream();
      out = socket.getoutputstream();
    } catch (ioexception e) {
      throw new runtimeexception("连接到 " + smtpserver + ":" + smtpport + " 失败", e);
    }
 
    bufferedreaderproxy reader = new bufferedreaderproxy(new inputstreamreader(in), showlog);
    printwriterproxy writer = new printwriterproxy(out, showlog);
 
    reader.showresponse();
    writer.println("helo " + smtpserver);
    reader.showresponse();
    writer.println("auth login");
    reader.showresponse();
    writer.println(new string(base64.encodebase64(username.getbytes())));
    reader.showresponse();
    writer.println(new string(base64.encodebase64(password.getbytes())));
    reader.showresponse();
    writer.println("mail from:" + frommail);
    reader.showresponse();
    for (string mail : tomail) {
      writer.println("rcpt to:" + mail);
      reader.showresponse();
    }
    writer.println("data");
    writer.println("content-type:text/html");
    if (subject != null) {
      writer.println("subject:" + subject);
    }
    writer.println("from:" + frommail);
    writer.print("to:");
    for (string mail : tomail) {
      writer.print(mail + "; ");
    }
    writer.println();
    writer.println();
    writer.println(content);
    writer.println(".");
    reader.showresponse();
    writer.println("quit");
    reader.showresponse();
    try {
      socket.close();
    } catch (ioexception e) {
      system.err.println("发送邮件完成,关闭 socket 出错:" + e.getmessage());
    }
  }
 
  public string getsubject() {
    return subject;
  }
 
  public void setsubject(string subject) {
    this.subject = subject;
  }
 
  public string getfrommail() {
    return frommail;
  }
 
  public void setfrommail(string frommail) {
    this.frommail = frommail;
  }
 
  public string getsmtpserver() {
    return smtpserver;
  }
 
  public void setsmtpserver(string smtpserver) {
    this.smtpserver = smtpserver;
  }
 
  public int getsmtpport() {
    return smtpport;
  }
 
  public void setsmtpport(int smtpport) {
    this.smtpport = smtpport;
  }
 
  public string getcontent() {
    return content;
  }
 
  public void setcontent(string content) {
    this.content = content;
  }
 
  public list<string> gettomail() {
    return tomail;
  }
 
  public void settomail(list<string> tomail) {
    this.tomail = tomail;
  }
 
  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 getshowlog() {
    return showlog;
  }
 
  public void setshowlog(boolean showlog) {
    this.showlog = showlog;
  }
 
  static class printwriterproxy extends printwriter {
    private boolean showrequest;
 
    public printwriterproxy(outputstream out, boolean showrequest) {
      super(out, true);
      this.showrequest = showrequest;
    }
 
    @override
    public void println() {
      if (showrequest)
        system.out.println();
      super.println();
    }
 
    public void print(string s) {
      if (showrequest)
        system.out.print(s);
      super.print(s);
    }
  }
 
  static class bufferedreaderproxy extends bufferedreader {
    private boolean showresponse = true;
 
    public bufferedreaderproxy(reader in, boolean showresponse) {
      super(in);
      this.showresponse = showresponse;
    }
 
    public void showresponse() {
      try {
        string line = readline();
        string number = line.substring(0, 3);
        int num = -1;
        try {
          num = integer.parseint(number);
        } catch (exception e) {
        }
        if (num == -1) {
          throw new runtimeexception("响应信息错误 : " + line);
        } else if (num >= 400) {
          throw new runtimeexception("发送邮件失败 : " + line);
        }
        if (showresponse) {
          system.out.println(line);
        }
      } catch (ioexception e) {
        system.out.println("获取响应失败");
      }
    }
 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。