最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java使用Socket實現(xiàn)SMTP協(xié)議發(fā)送郵件

 更新時間:2016年05月29日 11:01:34   作者:IamOkay  
這篇文章主要為大家詳細(xì)介紹了java使用Socket實現(xiàn)SMTP協(xié)議發(fā)送郵件的相關(guān)資料,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java 利用Socket實現(xiàn)SMTP協(xié)議發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下

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("程序結(jié)束");
  }
 
  /** 郵件主題 **/
  private String subject;
  /** 從此地址發(fā)出 **/
  private String fromMail;
  /** 用戶名 **/
  private String userName;
  /** 登錄密碼 **/
  private String password;
  /** SMTP 服務(wù)器地址 **/
  private String smtpServer;
  /** SMTP 服務(wù)器端口(默認(rèn):25) **/
  private int smtpPort = 25;
  /** 發(fā)送到 toMail 中的所有地址 **/
  private List<String> toMail;
  /** 郵件內(nèi)容 **/
  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("發(fā)送郵件完成,關(guān)閉 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("響應(yīng)信息錯誤 : " + line);
        } else if (num >= 400) {
          throw new RuntimeException("發(fā)送郵件失敗 : " + line);
        }
        if (showResponse) {
          System.out.println(line);
        }
      } catch (IOException e) {
        System.out.println("獲取響應(yīng)失敗");
      }
    }
 
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中關(guān)于static和templates的注意事項以及webjars的配置

    SpringBoot中關(guān)于static和templates的注意事項以及webjars的配置

    今天小編就為大家分享一篇關(guān)于SpringBoot中關(guān)于static和templates的注意事項以及webjars的配置,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 關(guān)于maven項目引入maven庫沒有的jar處理辦法

    關(guān)于maven項目引入maven庫沒有的jar處理辦法

    這篇文章主要介紹了關(guān)于maven項目引入maven庫沒有的jar處理辦法,在平時開發(fā)中,有些jar包是不存在maven中央庫中的,那么此時該如何解決才能方便后續(xù)處理呢,需要的朋友可以參考下本文
    2023-03-03
  • Java 給PDF簽名時添加可信時間戳的方法

    Java 給PDF簽名時添加可信時間戳的方法

    這篇文章主要介紹了Java 給PDF簽名時添加可信時間戳,關(guān)于jar導(dǎo)入的問題,本文給大家?guī)韮煞N方法,一種是手動導(dǎo)入另一種是maven配置導(dǎo)入,需要的朋友可以參考下
    2021-07-07
  • Springboot整合第三方登錄功能的實現(xiàn)示例

    Springboot整合第三方登錄功能的實現(xiàn)示例

    本文主要介紹了Springboot整合第三方登錄功能的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • java使用udp實現(xiàn)簡單多人聊天功能

    java使用udp實現(xiàn)簡單多人聊天功能

    這篇文章主要為大家詳細(xì)介紹了java使用udp實現(xiàn)簡單多人聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java設(shè)計模式中代理模式應(yīng)用詳解

    Java設(shè)計模式中代理模式應(yīng)用詳解

    代理模式(Proxy Parttern)為一個對象提供一個替身,來控制這個對象的訪問,即通過代理對象來訪問目標(biāo)對象。本文將通過示例詳細(xì)講解一下這個模式,需要的可以參考一下
    2022-11-11
  • java?IP歸屬地功能實現(xiàn)詳解

    java?IP歸屬地功能實現(xiàn)詳解

    前一陣子抖音和微博開始陸續(xù)上了IP歸屬地的功能,引起了眾多熱議,有大批在國外的老鐵們開始"原形畢露",被定位到國內(nèi)來,那么IP歸屬到底是怎么實現(xiàn)的呢?那么網(wǎng)紅們的歸屬地到底對不對呢
    2022-07-07
  • LoggingEventAsyncDisruptorAppender類執(zhí)行流程源碼解讀

    LoggingEventAsyncDisruptorAppender類執(zhí)行流程源碼解讀

    這篇文章主要介紹了LoggingEventAsyncDisruptorAppender類執(zhí)行流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • SpringBoot3使用?自定義注解+Jackson實現(xiàn)接口數(shù)據(jù)脫敏的步驟

    SpringBoot3使用?自定義注解+Jackson實現(xiàn)接口數(shù)據(jù)脫敏的步驟

    本文介紹了一種以優(yōu)雅的方式實現(xiàn)對接口返回的敏感數(shù)據(jù),如手機號、郵箱、身份證等信息的脫敏處理,這種方法也是企業(yè)常用方法,話不多說我們一起來看一下吧
    2024-03-03
  • JavaScript中棧和隊列應(yīng)用詳情

    JavaScript中棧和隊列應(yīng)用詳情

    這篇文章主要介紹了JavaScript中棧和隊列應(yīng)用詳情,棧如果用數(shù)組模擬的話是類似于一個U形桶狀堆??臻g,文章圍繞制圖展開詳細(xì)的內(nèi)容展開更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06

最新評論

荣昌县| 华容县| 饶阳县| 岑巩县| 昌图县| 永春县| 福海县| 武定县| 濮阳市| 徐汇区| 四会市| 电白县| 荃湾区| 涡阳县| 铁岭市| 张家界市| 永登县| 濮阳市| 新邵县| 中西区| 常州市| 南部县| 盖州市| 四平市| 乐清市| 济南市| 靖西县| 吴桥县| 永康市| 柘城县| 高青县| 延津县| 梁平县| 靖远县| 依兰县| 开原市| 吴桥县| 西华县| 奉新县| 奉新县| 天峻县|