一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java使用Socket實現SMTP協議發送郵件

java使用Socket實現SMTP協議發送郵件

2020-05-07 11:56IamOkay JAVA教程

這篇文章主要為大家詳細介紹了java使用Socket實現SMTP協議發送郵件的相關資料,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java 利用Socket實現SMTP協議發送郵件的具體代碼,供大家參考,具體內容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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("獲取響應失敗");
      }
    }
 
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲欧美综合区自拍另类 | 毛片免费观看 | 99视频久久精品久久 | 国内精品免费 | 视频二区 素人 制服 国产 | 嫩草成人影院 | 果冻传媒在线视频观看免费 | 日韩无遮挡大尺度啪啪影片 | 千金肉奴隶在线观看 | 情欲满载2012美国dvd | 百合女女师生play黄肉黄 | 人禽l交免费视频观看+视频 | 天天操婷婷 | a级亚洲片精品久久久久久久 | 亚洲色欲色欲综合网站 | 亚洲欧美日韩另类精品一区二区三区 | 91天堂影院| www.91麻豆 | 日本免费的一级绿象 | 欧美精品一区视频 | 夫妇交换小说 | 久久人妻熟女中文字幕AV蜜芽 | 国产精品永久免费视频观看 | 538精品视频 | 好大~好爽~再进去一点 | 日本高清视频一区二区 | 极品久久 | 色国产精品| 日本一区二区三区四区无限 | 亚洲精品丝袜在线一区波多野结衣 | 糖心vlog视频永久破解版 | 暴露狂婷婷 | 国产亚洲毛片在线 | 国产专区日韩精品欧美色 | 亚洲精品αv一区二区三区 亚洲精品91大神在线观看 | 国产欧美在线播放 | 性色欲情网站IWWW九文堂 | 国产一区二区三区四区波多野结衣 | 亚洲精品6久久久久中文字幕 | 国产成+人+综合+欧美 亚洲 | 日韩欧美成末人一区二区三区 |