[Java] java邮件发送业务代码 →→→→→进入此内容的聊天室

来自 , 2020-09-06, 写在 Java, 查看 117 次.
URL http://www.code666.cn/view/5807a685
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import java.util.StringTokenizer;
  9.  
  10. import sun.misc.BASE64Encoder;
  11.  
  12. public class SendMailImp implements SendMail {
  13.     public static final int SINA = 0;
  14.     public static final int QQ = 1;
  15.     public static final int FOXMAIL = 2;
  16.     public static final int WANGYI163 = 3;
  17.     public static final int WANGYI126 = 8;
  18.     public static final int YEAH = 4;
  19.     public static final int HOTMAIL = 5;
  20.     public static final int YAHOO = 6;
  21.     public static final int GMAIL = 7;
  22.  
  23.     /** 用于向服务器发起连接 */
  24.     private Socket socket = null;
  25.     /** 与服务器连接后的输入流 */
  26.     private BufferedReader input = null;
  27.     /** 与服务器连接后的输出流 */
  28.     private BufferedWriter output = null;
  29.     /** 发送邮件服务器名称 */
  30.     private String server = null;
  31.     /** 发送端口号 */
  32.     private int port;
  33.     /** 邮件信息 */
  34.     private MailMessage message;
  35.  
  36.     public MailMessage getMessage() {
  37.         return message;
  38.     }
  39.  
  40.     //设置邮件的信息内容,以方法链的方式实现
  41.     public SendMailImp setMessage (MailMessage message) {
  42.         this.message = message;
  43.         return this;
  44.     }
  45.  
  46.     public Socket getSocket() {
  47.         return socket;
  48.     }
  49.  
  50.     public void setSocket (Socket socket) {
  51.         this.socket = socket;
  52.     }
  53.  
  54.     public BufferedReader getInput() {
  55.         return input;
  56.     }
  57.  
  58.     public void setInput (BufferedReader input) {
  59.         this.input = input;
  60.     }
  61.  
  62.     public BufferedWriter getOutput() {
  63.         return output;
  64.     }
  65.  
  66.     public void setOutput (BufferedWriter output) {
  67.         this.output = output;
  68.     }
  69.  
  70.     public String getServer() {
  71.         return server;
  72.     }
  73.  
  74.     public void setServer (String server) {
  75.         this.server = server;
  76.     }
  77.  
  78.     public int getPort() {
  79.         return port;
  80.     }
  81.  
  82.     public void setPort (int port) {
  83.         this.port = port;
  84.     }
  85.  
  86.     /**
  87.      * 根据邮箱类型,设置发送邮件服务器和端口号
  88.      */
  89.     public static SendMailImp getInstance (int sendMailType) {
  90.         SendMailImp sendMailImp = new SendMailImp();
  91.         switch (sendMailType) {
  92.         case SINA:
  93.             sendMailImp.setServer ("smtp.sina.com");
  94.             sendMailImp.setPort (25);
  95.             break;
  96.         case QQ:
  97.         case FOXMAIL:
  98.             sendMailImp.setServer ("smtp.qq.com");
  99.             sendMailImp.setPort (25);
  100.             break;
  101.         case WANGYI126:
  102.             sendMailImp.setServer ("smtp.126.com");
  103.             sendMailImp.setPort (25);
  104.             break;
  105.         case WANGYI163:
  106.             sendMailImp.setServer ("smtp.163.com");
  107.             sendMailImp.setPort (25);
  108.             break;
  109.         case YEAH:
  110.             sendMailImp.setServer ("smtp.yeah.net");
  111.             sendMailImp.setPort (25);
  112.             break;
  113.         case YAHOO:
  114.             sendMailImp.setServer ("smtp.mail.yahoo.com");
  115.             sendMailImp.setPort (465);
  116.             break;
  117.         case GMAIL:
  118.             sendMailImp.setServer ("smtp.gmail.com");
  119.             sendMailImp.setPort (465);
  120.             break;
  121.         case HOTMAIL:
  122.             sendMailImp.setServer ("smtp.live.com");
  123.             sendMailImp.setPort (25);
  124.             break;
  125.         default:
  126.             break;
  127.         }
  128.  
  129.         try {
  130.             // 与发送邮件服务器建立Socket连接,并得到输入输出流
  131.             Socket socket = new Socket (sendMailImp.getServer(), sendMailImp
  132.                                         .getPort() );
  133.             BufferedReader input = new BufferedReader (new InputStreamReader (
  134.                         socket.getInputStream() ) );
  135.             BufferedWriter output = new BufferedWriter (new OutputStreamWriter (
  136.                         socket.getOutputStream() ) );
  137.  
  138.             sendMailImp.setSocket (socket);
  139.             sendMailImp.setInput (input);
  140.             sendMailImp.setOutput (output);
  141.         } catch (UnknownHostException e) {
  142.             e.printStackTrace();
  143.         } catch (IOException e) {
  144.             e.printStackTrace();
  145.         }
  146.  
  147.         return sendMailImp;
  148.     }
  149.  
  150.     public int sendToServer (String order) {
  151.         try {
  152.             this.getOutput().write (order);
  153.             this.getOutput().newLine();
  154.             this.getOutput().flush();
  155.  
  156.             System.out.println ("已发送命令:" + order);
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.         }
  160.  
  161.         return getResult();
  162.     }
  163.  
  164.     public int getResult() {
  165.         String line = "";
  166.         try {
  167.             line = this.getInput().readLine();
  168.             System.out.println ("服务器返回状态:" + line);
  169.         } catch (IOException e) {
  170.             e.printStackTrace();
  171.         }
  172.  
  173.         // 从服务器返回消息中取得状态码,并转换成整数返回
  174.         StringTokenizer get = new StringTokenizer (line, " ");
  175.         return Integer.parseInt (get.nextToken() );
  176.     }
  177.  
  178.     public void regist() throws IOException {
  179.         int result = getResult();
  180.         // 连接上服务器后,服务器给出220应答
  181.         if (result != 220) {
  182.             throw new IOException ("连接服务器失败");
  183.         }
  184.  
  185.         result = sendToServer ("HELO " + this.getServer() );
  186.         // HELO命令成功后,服务器返回250应答
  187.         if (result != 250) {
  188.             throw new IOException ("注册邮件服务器失败");
  189.         }
  190.     }
  191.  
  192.     public void login() throws IOException {
  193.         BASE64Encoder encode = new BASE64Encoder();
  194.  
  195.         int result = sendToServer ("AUTH LOGIN");
  196.         // 登陆服务成功,服务器返回334应答
  197.         if (result != 334) {
  198.             throw new IOException ("登陆服务失败");
  199.         }
  200.  
  201.         // 对邮箱用户名进行验证
  202.         result = sendToServer (encode.encode (this.message.getUser().getBytes() ) );
  203.         if (result != 334) {
  204.             throw new IOException ("用户名错误");
  205.         }
  206.  
  207.         // 对邮箱密码进行验证
  208.         result = sendToServer (encode.encode (message.getPassword().getBytes() ) );
  209.         if (result != 235) {
  210.             throw new IOException ("用户验证错误");
  211.         }
  212.     }
  213.  
  214.     public void setMailFrom() throws IOException {
  215.         int result = sendToServer ("MAIL FROM:<" + this.getMessage().getFrom()
  216.         + ">");
  217.         if (result != 250) {
  218.             throw new IOException ("邮件源地址错误");
  219.         }
  220.     }
  221.  
  222.     public void setMailTo() throws IOException {
  223.         int result = sendToServer ("RCPT TO:<" + this.getMessage().getTo() + ">");
  224.         if (result != 250) {
  225.             throw new IOException ("邮件目的地址错误");
  226.         }
  227.     }
  228.  
  229.     public void setData() throws IOException {
  230.         int result = sendToServer ("DATA");
  231.         // 输入date回车后,若收到354应答后,继续输入邮件内容
  232.         if (result != 354) {
  233.             throw new IOException ("不能发送数据");
  234.         }
  235.  
  236.         this.getOutput().write ("FROM:" + this.getMessage().getDatafrom() );
  237.         this.getOutput().newLine();
  238.         this.getOutput().write ("TO:" + this.getMessage().getDatato() );
  239.         this.getOutput().newLine();
  240.         this.getOutput().write ("SUBJECT:" + this.getMessage().getSubject() );
  241.         this.getOutput().newLine();
  242.         this.getOutput().newLine();
  243.         this.getOutput().write (this.getMessage().getContent() );
  244.         this.getOutput().newLine();
  245.  
  246.         // 句点加回车结束邮件内容输入
  247.         result = sendToServer (".");
  248.         if (result != 250) {
  249.             throw new IOException ("发送数据错误");
  250.         }
  251.     }
  252.  
  253.     public void quit() throws IOException {
  254.         int result = sendToServer ("QUIT");
  255.         if (result != 221) {
  256.             throw new IOException ("未能正确退出");
  257.         }
  258.  
  259.         this.getInput().close();
  260.         this.getOutput().close();
  261.     }
  262.  
  263.     public boolean sendMail() {
  264.         boolean success = true;
  265.         try {
  266.             // 注册到发送邮件服务器
  267.             regist();
  268.             // 登陆到服务器
  269.             login();
  270.             // 设置邮件源地址
  271.             setMailFrom();
  272.             // 设置邮件目的地址
  273.             setMailTo();
  274.             // 设置邮件体
  275.             setData();
  276.             // 退出邮件
  277.             quit();
  278.         } catch (Exception e) {
  279.             e.printStackTrace();
  280.             success = false;
  281.         }
  282.         return success;
  283.     }
  284. }

回复 "java邮件发送业务代码"

这儿你可以回复上面这条便签

captcha