|
@@ -0,0 +1,561 @@
|
|
1
|
+Imports System
|
|
2
|
+Imports System.IO
|
|
3
|
+Imports System.Net
|
|
4
|
+Imports System.Net.Mail
|
|
5
|
+Imports System.Text
|
|
6
|
+Imports System.Threading.Tasks
|
|
7
|
+Imports System.Windows.Forms
|
|
8
|
+
|
|
9
|
+Public Class SmtpTestForm
|
|
10
|
+ Inherits Form
|
|
11
|
+
|
|
12
|
+ ' UI 控制項宣告
|
|
13
|
+ Private grpServer As GroupBox
|
|
14
|
+ Private lblServer As Label
|
|
15
|
+ Private txtServer As TextBox
|
|
16
|
+ Private lblPort As Label
|
|
17
|
+ Private txtPort As TextBox
|
|
18
|
+ Private chkSSL As CheckBox
|
|
19
|
+ Private chkAuth As CheckBox
|
|
20
|
+
|
|
21
|
+ Private grpAuth As GroupBox
|
|
22
|
+ Private lblUsername As Label
|
|
23
|
+ Private txtUsername As TextBox
|
|
24
|
+ Private lblPassword As Label
|
|
25
|
+ Private txtPassword As TextBox
|
|
26
|
+
|
|
27
|
+ Private grpEmail As GroupBox
|
|
28
|
+ Private lblFrom As Label
|
|
29
|
+ Private txtFrom As TextBox
|
|
30
|
+ Private lblTo As Label
|
|
31
|
+ Private txtTo As TextBox
|
|
32
|
+ Private lblSubject As Label
|
|
33
|
+ Private txtSubject As TextBox
|
|
34
|
+ Private lblBody As Label
|
|
35
|
+ Private txtBody As TextBox
|
|
36
|
+
|
|
37
|
+ Private grpOptions As GroupBox
|
|
38
|
+ Private btnSmtpTest As Button
|
|
39
|
+ Private btnSendTest As Button
|
|
40
|
+ Private btnSaveSettings As Button
|
|
41
|
+ Private btnLoadSettings As Button
|
|
42
|
+
|
|
43
|
+ Private txtLog As TextBox
|
|
44
|
+
|
|
45
|
+ ' 配置文件路徑
|
|
46
|
+ Private ReadOnly ConfigFile As String = Path.Combine(Application.StartupPath, "smtp_config.ini")
|
|
47
|
+
|
|
48
|
+ Public Sub New()
|
|
49
|
+ InitializeComponent00()
|
|
50
|
+ LoadSettings()
|
|
51
|
+ End Sub
|
|
52
|
+
|
|
53
|
+ Private Sub InitializeComponent00()
|
|
54
|
+ ' 設定窗體屬性
|
|
55
|
+ Me.Text = "內部郵件發送測試工具"
|
|
56
|
+ Me.Size = New Size(700, 650)
|
|
57
|
+ Me.StartPosition = FormStartPosition.CenterScreen
|
|
58
|
+ Me.FormBorderStyle = FormBorderStyle.FixedDialog
|
|
59
|
+ Me.MaximizeBox = False
|
|
60
|
+
|
|
61
|
+ ' 伺服器配置區域
|
|
62
|
+ grpServer = New GroupBox()
|
|
63
|
+ grpServer.Text = "SMTP 伺服器設定"
|
|
64
|
+ grpServer.Location = New Point(12, 12)
|
|
65
|
+ grpServer.Size = New Size(320, 140)
|
|
66
|
+
|
|
67
|
+ lblServer = New Label()
|
|
68
|
+ lblServer.Text = "伺服器地址:"
|
|
69
|
+ lblServer.Location = New Point(15, 30)
|
|
70
|
+ lblServer.Size = New Size(100, 20)
|
|
71
|
+
|
|
72
|
+ txtServer = New TextBox()
|
|
73
|
+ txtServer.Location = New Point(120, 30)
|
|
74
|
+ txtServer.Size = New Size(180, 23)
|
|
75
|
+ txtServer.Text = "mail.yourcompany.com"
|
|
76
|
+
|
|
77
|
+ lblPort = New Label()
|
|
78
|
+ lblPort.Text = "伺服器端口:"
|
|
79
|
+ lblPort.Location = New Point(15, 60)
|
|
80
|
+ lblPort.Size = New Size(100, 20)
|
|
81
|
+
|
|
82
|
+ txtPort = New TextBox()
|
|
83
|
+ txtPort.Location = New Point(120, 60)
|
|
84
|
+ txtPort.Size = New Size(180, 23)
|
|
85
|
+ txtPort.Text = "25"
|
|
86
|
+
|
|
87
|
+ chkSSL = New CheckBox()
|
|
88
|
+ chkSSL.Text = "使用 SSL/TLS 加密連接"
|
|
89
|
+ chkSSL.Location = New Point(15, 90)
|
|
90
|
+ chkSSL.Size = New Size(200, 20)
|
|
91
|
+ chkSSL.Checked = False
|
|
92
|
+
|
|
93
|
+ chkAuth = New CheckBox()
|
|
94
|
+ chkAuth.Text = "需要身份認證"
|
|
95
|
+ chkAuth.Location = New Point(15, 110)
|
|
96
|
+ chkAuth.Size = New Size(200, 20)
|
|
97
|
+ chkAuth.Checked = True
|
|
98
|
+ AddHandler chkAuth.CheckedChanged, AddressOf ChkAuth_CheckedChanged
|
|
99
|
+
|
|
100
|
+ grpServer.Controls.Add(lblServer)
|
|
101
|
+ grpServer.Controls.Add(txtServer)
|
|
102
|
+ grpServer.Controls.Add(lblPort)
|
|
103
|
+ grpServer.Controls.Add(txtPort)
|
|
104
|
+ grpServer.Controls.Add(chkSSL)
|
|
105
|
+ grpServer.Controls.Add(chkAuth)
|
|
106
|
+
|
|
107
|
+ ' 認證配置區域
|
|
108
|
+ grpAuth = New GroupBox()
|
|
109
|
+ grpAuth.Text = "認證信息"
|
|
110
|
+ grpAuth.Location = New Point(350, 12)
|
|
111
|
+ grpAuth.Size = New Size(320, 140)
|
|
112
|
+
|
|
113
|
+ lblUsername = New Label()
|
|
114
|
+ lblUsername.Text = "用戶名:"
|
|
115
|
+ lblUsername.Location = New Point(15, 30)
|
|
116
|
+ lblUsername.Size = New Size(100, 20)
|
|
117
|
+
|
|
118
|
+ txtUsername = New TextBox()
|
|
119
|
+ txtUsername.Location = New Point(120, 30)
|
|
120
|
+ txtUsername.Size = New Size(180, 23)
|
|
121
|
+ txtUsername.Text = "user@yourcompany.com"
|
|
122
|
+
|
|
123
|
+ lblPassword = New Label()
|
|
124
|
+ lblPassword.Text = "密碼:"
|
|
125
|
+ lblPassword.Location = New Point(15, 60)
|
|
126
|
+ lblPassword.Size = New Size(100, 20)
|
|
127
|
+
|
|
128
|
+ txtPassword = New TextBox()
|
|
129
|
+ txtPassword.Location = New Point(120, 60)
|
|
130
|
+ txtPassword.Size = New Size(180, 23)
|
|
131
|
+ txtPassword.PasswordChar = "*"c
|
|
132
|
+
|
|
133
|
+ grpAuth.Controls.Add(lblUsername)
|
|
134
|
+ grpAuth.Controls.Add(txtUsername)
|
|
135
|
+ grpAuth.Controls.Add(lblPassword)
|
|
136
|
+ grpAuth.Controls.Add(txtPassword)
|
|
137
|
+
|
|
138
|
+ ' 郵件內容區域
|
|
139
|
+ grpEmail = New GroupBox()
|
|
140
|
+ grpEmail.Text = "郵件內容"
|
|
141
|
+ grpEmail.Location = New Point(12, 160)
|
|
142
|
+ grpEmail.Size = New Size(658, 220)
|
|
143
|
+
|
|
144
|
+ lblFrom = New Label()
|
|
145
|
+ lblFrom.Text = "發件人:"
|
|
146
|
+ lblFrom.Location = New Point(15, 30)
|
|
147
|
+ lblFrom.Size = New Size(100, 20)
|
|
148
|
+
|
|
149
|
+ txtFrom = New TextBox()
|
|
150
|
+ txtFrom.Location = New Point(120, 30)
|
|
151
|
+ txtFrom.Size = New Size(520, 23)
|
|
152
|
+ txtFrom.Text = "alerts@yourcompany.com"
|
|
153
|
+
|
|
154
|
+ lblTo = New Label()
|
|
155
|
+ lblTo.Text = "收件人:"
|
|
156
|
+ lblTo.Location = New Point(15, 60)
|
|
157
|
+ lblTo.Size = New Size(100, 20)
|
|
158
|
+
|
|
159
|
+ txtTo = New TextBox()
|
|
160
|
+ txtTo.Location = New Point(120, 60)
|
|
161
|
+ txtTo.Size = New Size(520, 23)
|
|
162
|
+ txtTo.Text = "recipient@yourcompany.com"
|
|
163
|
+
|
|
164
|
+ lblSubject = New Label()
|
|
165
|
+ lblSubject.Text = "主旨:"
|
|
166
|
+ lblSubject.Location = New Point(15, 90)
|
|
167
|
+ lblSubject.Size = New Size(100, 20)
|
|
168
|
+
|
|
169
|
+ txtSubject = New TextBox()
|
|
170
|
+ txtSubject.Location = New Point(120, 90)
|
|
171
|
+ txtSubject.Size = New Size(520, 23)
|
|
172
|
+ txtSubject.Text = "測試郵件"
|
|
173
|
+
|
|
174
|
+ lblBody = New Label()
|
|
175
|
+ lblBody.Text = "內容:"
|
|
176
|
+ lblBody.Location = New Point(15, 120)
|
|
177
|
+ lblBody.Size = New Size(100, 20)
|
|
178
|
+
|
|
179
|
+ txtBody = New TextBox()
|
|
180
|
+ txtBody.Location = New Point(120, 120)
|
|
181
|
+ txtBody.Size = New Size(520, 80)
|
|
182
|
+ txtBody.Multiline = True
|
|
183
|
+ txtBody.ScrollBars = ScrollBars.Vertical
|
|
184
|
+ txtBody.Text = "這是一封測試郵件,用於驗證 SMTP 功能。" & vbCrLf & vbCrLf & "發送時間: " & DateTime.Now.ToString()
|
|
185
|
+
|
|
186
|
+ grpEmail.Controls.Add(lblFrom)
|
|
187
|
+ grpEmail.Controls.Add(txtFrom)
|
|
188
|
+ grpEmail.Controls.Add(lblTo)
|
|
189
|
+ grpEmail.Controls.Add(txtTo)
|
|
190
|
+ grpEmail.Controls.Add(lblSubject)
|
|
191
|
+ grpEmail.Controls.Add(txtSubject)
|
|
192
|
+ grpEmail.Controls.Add(lblBody)
|
|
193
|
+ grpEmail.Controls.Add(txtBody)
|
|
194
|
+
|
|
195
|
+ ' 操作按鈕區域
|
|
196
|
+ grpOptions = New GroupBox()
|
|
197
|
+ grpOptions.Text = "操作"
|
|
198
|
+ grpOptions.Location = New Point(12, 390)
|
|
199
|
+ grpOptions.Size = New Size(658, 70)
|
|
200
|
+
|
|
201
|
+ btnSmtpTest = New Button()
|
|
202
|
+ btnSmtpTest.Text = "測試 SMTP 連接"
|
|
203
|
+ btnSmtpTest.Location = New Point(15, 25)
|
|
204
|
+ btnSmtpTest.Size = New Size(150, 30)
|
|
205
|
+ AddHandler btnSmtpTest.Click, AddressOf BtnSmtpTest_Click
|
|
206
|
+
|
|
207
|
+ btnSendTest = New Button()
|
|
208
|
+ btnSendTest.Text = "發送測試郵件"
|
|
209
|
+ btnSendTest.Location = New Point(175, 25)
|
|
210
|
+ btnSendTest.Size = New Size(150, 30)
|
|
211
|
+ AddHandler btnSendTest.Click, AddressOf BtnSendTest_Click
|
|
212
|
+
|
|
213
|
+ btnSaveSettings = New Button()
|
|
214
|
+ btnSaveSettings.Text = "保存設定"
|
|
215
|
+ btnSaveSettings.Location = New Point(335, 25)
|
|
216
|
+ btnSaveSettings.Size = New Size(150, 30)
|
|
217
|
+ AddHandler btnSaveSettings.Click, AddressOf BtnSaveSettings_Click
|
|
218
|
+
|
|
219
|
+ btnLoadSettings = New Button()
|
|
220
|
+ btnLoadSettings.Text = "加載設定"
|
|
221
|
+ btnLoadSettings.Location = New Point(495, 25)
|
|
222
|
+ btnLoadSettings.Size = New Size(150, 30)
|
|
223
|
+ AddHandler btnLoadSettings.Click, AddressOf BtnLoadSettings_Click
|
|
224
|
+
|
|
225
|
+ grpOptions.Controls.Add(btnSmtpTest)
|
|
226
|
+ grpOptions.Controls.Add(btnSendTest)
|
|
227
|
+ grpOptions.Controls.Add(btnSaveSettings)
|
|
228
|
+ grpOptions.Controls.Add(btnLoadSettings)
|
|
229
|
+
|
|
230
|
+ ' 日誌區域
|
|
231
|
+ txtLog = New TextBox()
|
|
232
|
+ txtLog.Multiline = True
|
|
233
|
+ txtLog.ScrollBars = ScrollBars.Both
|
|
234
|
+ txtLog.ReadOnly = True
|
|
235
|
+ txtLog.Location = New Point(12, 470)
|
|
236
|
+ txtLog.Size = New Size(658, 130)
|
|
237
|
+ txtLog.BackColor = Color.FromArgb(240, 240, 240)
|
|
238
|
+
|
|
239
|
+ ' 添加控件到窗體
|
|
240
|
+ Me.Controls.Add(grpServer)
|
|
241
|
+ Me.Controls.Add(grpAuth)
|
|
242
|
+ Me.Controls.Add(grpEmail)
|
|
243
|
+ Me.Controls.Add(grpOptions)
|
|
244
|
+ Me.Controls.Add(txtLog)
|
|
245
|
+
|
|
246
|
+ ' 初始化界面狀態
|
|
247
|
+ UpdateAuthUIState()
|
|
248
|
+ End Sub
|
|
249
|
+
|
|
250
|
+ Private Sub ChkAuth_CheckedChanged(sender As Object, e As EventArgs)
|
|
251
|
+ UpdateAuthUIState()
|
|
252
|
+ End Sub
|
|
253
|
+
|
|
254
|
+ Private Sub UpdateAuthUIState()
|
|
255
|
+ ' 根據是否需要認證來啟用/禁用認證信息輸入
|
|
256
|
+ grpAuth.Enabled = chkAuth.Checked
|
|
257
|
+ End Sub
|
|
258
|
+
|
|
259
|
+ Private Sub BtnSmtpTest_Click(sender As Object, e As EventArgs)
|
|
260
|
+ LogMessage("開始測試 SMTP 連接...")
|
|
261
|
+ TestSmtpConnection()
|
|
262
|
+ End Sub
|
|
263
|
+
|
|
264
|
+ Private Async Sub BtnSendTest_Click(sender As Object, e As EventArgs)
|
|
265
|
+ LogMessage("開始發送測試郵件...")
|
|
266
|
+
|
|
267
|
+ ' 更新郵件內容中的時間戳
|
|
268
|
+ If Not txtBody.Text.Contains("發送時間:") Then
|
|
269
|
+ txtBody.Text &= vbCrLf & vbCrLf & "發送時間: "
|
|
270
|
+ End If
|
|
271
|
+
|
|
272
|
+ txtBody.Text = txtBody.Text.Replace(txtBody.Text.Substring(txtBody.Text.IndexOf("發送時間:")),
|
|
273
|
+ "發送時間: " & DateTime.Now.ToString())
|
|
274
|
+
|
|
275
|
+ ' 發送測試郵件
|
|
276
|
+ Await SendTestEmailAsync()
|
|
277
|
+ End Sub
|
|
278
|
+
|
|
279
|
+ Private Sub BtnSaveSettings_Click(sender As Object, e As EventArgs)
|
|
280
|
+ SaveSettings()
|
|
281
|
+ End Sub
|
|
282
|
+
|
|
283
|
+ Private Sub BtnLoadSettings_Click(sender As Object, e As EventArgs)
|
|
284
|
+ LoadSettings()
|
|
285
|
+ End Sub
|
|
286
|
+
|
|
287
|
+ Private Sub SaveSettings()
|
|
288
|
+ Try
|
|
289
|
+ Using writer As New StreamWriter(ConfigFile, False)
|
|
290
|
+ writer.WriteLine("Server=" & txtServer.Text)
|
|
291
|
+ writer.WriteLine("Port=" & txtPort.Text)
|
|
292
|
+ writer.WriteLine("SSL=" & chkSSL.Checked.ToString())
|
|
293
|
+ writer.WriteLine("RequireAuth=" & chkAuth.Checked.ToString())
|
|
294
|
+ writer.WriteLine("Username=" & txtUsername.Text)
|
|
295
|
+ ' 注意:出於安全考慮,密碼不會以明文保存
|
|
296
|
+ ' 實際應用中應使用安全的加密方式存儲密碼
|
|
297
|
+ ' 這裡僅為演示,使用簡單的編碼
|
|
298
|
+ Dim encodedPassword As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(txtPassword.Text))
|
|
299
|
+ writer.WriteLine("Password=" & encodedPassword)
|
|
300
|
+ writer.WriteLine("From=" & txtFrom.Text)
|
|
301
|
+ writer.WriteLine("To=" & txtTo.Text)
|
|
302
|
+ End Using
|
|
303
|
+ LogMessage("設定已保存到: " & ConfigFile)
|
|
304
|
+ Catch ex As Exception
|
|
305
|
+ LogMessage("保存設定失敗: " & ex.Message)
|
|
306
|
+ End Try
|
|
307
|
+ End Sub
|
|
308
|
+
|
|
309
|
+ Private Sub LoadSettings()
|
|
310
|
+ If Not File.Exists(ConfigFile) Then
|
|
311
|
+ LogMessage("設定文件不存在,使用默認值")
|
|
312
|
+ Return
|
|
313
|
+ End If
|
|
314
|
+
|
|
315
|
+ Try
|
|
316
|
+ Dim settings As New Dictionary(Of String, String)()
|
|
317
|
+
|
|
318
|
+ Using reader As New StreamReader(ConfigFile)
|
|
319
|
+ While Not reader.EndOfStream
|
|
320
|
+ Dim line As String = reader.ReadLine()
|
|
321
|
+ If Not String.IsNullOrEmpty(line) AndAlso line.Contains("=") Then
|
|
322
|
+ Dim parts As String() = line.Split(New Char() {"="c}, 2)
|
|
323
|
+ If parts.Length = 2 Then
|
|
324
|
+ settings(parts(0)) = parts(1)
|
|
325
|
+ End If
|
|
326
|
+ End If
|
|
327
|
+ End While
|
|
328
|
+ End Using
|
|
329
|
+
|
|
330
|
+ If settings.ContainsKey("Server") Then txtServer.Text = settings("Server")
|
|
331
|
+ If settings.ContainsKey("Port") Then txtPort.Text = settings("Port")
|
|
332
|
+ If settings.ContainsKey("SSL") Then chkSSL.Checked = Boolean.Parse(settings("SSL"))
|
|
333
|
+ If settings.ContainsKey("RequireAuth") Then chkAuth.Checked = Boolean.Parse(settings("RequireAuth"))
|
|
334
|
+ If settings.ContainsKey("Username") Then txtUsername.Text = settings("Username")
|
|
335
|
+ If settings.ContainsKey("Password") Then
|
|
336
|
+ ' 解碼密碼
|
|
337
|
+ Try
|
|
338
|
+ Dim decodedBytes As Byte() = Convert.FromBase64String(settings("Password"))
|
|
339
|
+ txtPassword.Text = Encoding.UTF8.GetString(decodedBytes)
|
|
340
|
+ Catch
|
|
341
|
+ txtPassword.Text = ""
|
|
342
|
+ End Try
|
|
343
|
+ End If
|
|
344
|
+ If settings.ContainsKey("From") Then txtFrom.Text = settings("From")
|
|
345
|
+ If settings.ContainsKey("To") Then txtTo.Text = settings("To")
|
|
346
|
+
|
|
347
|
+ UpdateAuthUIState()
|
|
348
|
+ LogMessage("設定已從文件加載: " & ConfigFile)
|
|
349
|
+ Catch ex As Exception
|
|
350
|
+ LogMessage("加載設定失敗: " & ex.Message)
|
|
351
|
+ End Try
|
|
352
|
+ End Sub
|
|
353
|
+
|
|
354
|
+ Private Async Sub TestSmtpConnection()
|
|
355
|
+ Dim server As String = txtServer.Text.Trim()
|
|
356
|
+ Dim port As Integer
|
|
357
|
+
|
|
358
|
+ If String.IsNullOrEmpty(server) Then
|
|
359
|
+ LogMessage("錯誤: 請輸入 SMTP 伺服器地址")
|
|
360
|
+ Return
|
|
361
|
+ End If
|
|
362
|
+
|
|
363
|
+ If Not Integer.TryParse(txtPort.Text, port) OrElse port <= 0 OrElse port > 65535 Then
|
|
364
|
+ LogMessage("錯誤: 端口號無效,請輸入 1-65535 之間的數字")
|
|
365
|
+ Return
|
|
366
|
+ End If
|
|
367
|
+
|
|
368
|
+ Try
|
|
369
|
+ ' 首先嘗試 TCP 連接到指定端口
|
|
370
|
+ LogMessage("嘗試連接到 " & server & ":" & port & "...")
|
|
371
|
+
|
|
372
|
+ Await Task.Run(Sub()
|
|
373
|
+ Try
|
|
374
|
+ Using client As New System.Net.Sockets.TcpClient()
|
|
375
|
+ ' 嘗試連接,設置 5 秒超時
|
|
376
|
+ Dim result As IAsyncResult = client.BeginConnect(server, port, Nothing, Nothing)
|
|
377
|
+ Dim success As Boolean = result.AsyncWaitHandle.WaitOne(5000, True)
|
|
378
|
+
|
|
379
|
+ If success Then
|
|
380
|
+ Invoke(Sub() LogMessage("TCP 連接成功"))
|
|
381
|
+
|
|
382
|
+ ' 如果成功連接,建立一個 SMTP 客戶端進行完整測試
|
|
383
|
+ Invoke(Sub() LogMessage("嘗試 SMTP 協議測試..."))
|
|
384
|
+
|
|
385
|
+ Using smtpClient As New SmtpClient(server, port)
|
|
386
|
+ smtpClient.EnableSsl = chkSSL.Checked
|
|
387
|
+ smtpClient.Timeout = 10000 ' 10 秒超時
|
|
388
|
+
|
|
389
|
+ If chkAuth.Checked Then
|
|
390
|
+ smtpClient.Credentials = New NetworkCredential(txtUsername.Text, txtPassword.Text)
|
|
391
|
+ End If
|
|
392
|
+
|
|
393
|
+ ' 建立一個空郵件用於測試,不實際發送
|
|
394
|
+ Dim testMessage As New MailMessage()
|
|
395
|
+ testMessage.From = New MailAddress(txtFrom.Text)
|
|
396
|
+ testMessage.Subject = "測試連接 (不會發送)"
|
|
397
|
+
|
|
398
|
+ ' 不實際發送,只測試連接
|
|
399
|
+ ' 這裡我們不調用 Send,而是只測試當前連接狀態
|
|
400
|
+ End Using
|
|
401
|
+
|
|
402
|
+ Invoke(Sub() LogMessage("SMTP 連接測試成功!伺服器連接正常。"))
|
|
403
|
+ Else
|
|
404
|
+ Invoke(Sub() LogMessage("錯誤: 無法連接到伺服器,連接超時"))
|
|
405
|
+ client.Close()
|
|
406
|
+ End If
|
|
407
|
+ End Using
|
|
408
|
+ Catch ex As Exception
|
|
409
|
+ Invoke(Sub() LogMessage("連接測試失敗: " & ex.Message))
|
|
410
|
+ End Try
|
|
411
|
+ End Sub)
|
|
412
|
+ Catch ex As Exception
|
|
413
|
+ LogMessage("測試過程出錯: " & ex.Message)
|
|
414
|
+ End Try
|
|
415
|
+ End Sub
|
|
416
|
+
|
|
417
|
+ Private Async Function SendTestEmailAsync() As Task
|
|
418
|
+ Dim server As String = txtServer.Text.Trim()
|
|
419
|
+ Dim port As Integer
|
|
420
|
+
|
|
421
|
+ If String.IsNullOrEmpty(server) Then
|
|
422
|
+ LogMessage("錯誤: 請輸入 SMTP 伺服器地址")
|
|
423
|
+ Return
|
|
424
|
+ End If
|
|
425
|
+
|
|
426
|
+ If Not Integer.TryParse(txtPort.Text, port) OrElse port <= 0 OrElse port > 65535 Then
|
|
427
|
+ LogMessage("錯誤: 端口號無效,請輸入 1-65535 之間的數字")
|
|
428
|
+ Return
|
|
429
|
+ End If
|
|
430
|
+
|
|
431
|
+ If String.IsNullOrEmpty(txtFrom.Text) Then
|
|
432
|
+ LogMessage("錯誤: 請輸入發件人郵箱")
|
|
433
|
+ Return
|
|
434
|
+ End If
|
|
435
|
+
|
|
436
|
+ If String.IsNullOrEmpty(txtTo.Text) Then
|
|
437
|
+ LogMessage("錯誤: 請輸入收件人郵箱")
|
|
438
|
+ Return
|
|
439
|
+ End If
|
|
440
|
+
|
|
441
|
+ Try
|
|
442
|
+ LogMessage("準備發送郵件...")
|
|
443
|
+
|
|
444
|
+ ' 使用 Task.Run 避免阻塞 UI 線程
|
|
445
|
+ Await Task.Run(Sub()
|
|
446
|
+ Try
|
|
447
|
+ Invoke(Sub() LogMessage("配置 SMTP 客戶端..."))
|
|
448
|
+
|
|
449
|
+ ' 配置 SMTP 客戶端
|
|
450
|
+ Using smtpClient As New SmtpClient(server, port)
|
|
451
|
+ Invoke(Sub() LogMessage("設置 SMTP 參數..."))
|
|
452
|
+
|
|
453
|
+ smtpClient.EnableSsl = chkSSL.Checked
|
|
454
|
+ smtpClient.Timeout = 30000 ' 30 秒超時
|
|
455
|
+
|
|
456
|
+ If chkAuth.Checked Then
|
|
457
|
+ Invoke(Sub() LogMessage("使用認證..."))
|
|
458
|
+ smtpClient.Credentials = New NetworkCredential(txtUsername.Text, txtPassword.Text)
|
|
459
|
+ End If
|
|
460
|
+
|
|
461
|
+ ' 創建郵件
|
|
462
|
+ Invoke(Sub() LogMessage("創建郵件內容..."))
|
|
463
|
+
|
|
464
|
+ Using message As New MailMessage()
|
|
465
|
+ message.From = New MailAddress(txtFrom.Text)
|
|
466
|
+
|
|
467
|
+ ' 處理多個收件人(以逗號或分號分隔)
|
|
468
|
+ Dim toAddresses As String() = txtTo.Text.Split(New Char() {","c, ";"c}, StringSplitOptions.RemoveEmptyEntries)
|
|
469
|
+ For Each address As String In toAddresses
|
|
470
|
+ message.To.Add(address.Trim())
|
|
471
|
+ Next
|
|
472
|
+
|
|
473
|
+ message.Subject = txtSubject.Text
|
|
474
|
+ message.Body = txtBody.Text
|
|
475
|
+ message.BodyEncoding = Encoding.UTF8
|
|
476
|
+ message.IsBodyHtml = False
|
|
477
|
+
|
|
478
|
+ ' 詳細日誌記錄
|
|
479
|
+ Invoke(Sub() LogMessage("發送至: " & String.Join(", ", toAddresses)))
|
|
480
|
+ Invoke(Sub() LogMessage("主旨: " & txtSubject.Text))
|
|
481
|
+ Invoke(Sub() LogMessage("正在發送郵件..."))
|
|
482
|
+
|
|
483
|
+ ' 發送郵件
|
|
484
|
+ smtpClient.Send(message)
|
|
485
|
+
|
|
486
|
+ Invoke(Sub() LogMessage("郵件已成功發送!"))
|
|
487
|
+ End Using
|
|
488
|
+ End Using
|
|
489
|
+ Catch ex As Exception
|
|
490
|
+ Invoke(Sub() LogMessageWithDetails("發送郵件失敗", ex))
|
|
491
|
+ End Try
|
|
492
|
+ End Sub)
|
|
493
|
+ Catch ex As Exception
|
|
494
|
+ LogMessage("發送過程出錯: " & ex.Message)
|
|
495
|
+ End Try
|
|
496
|
+ End Function
|
|
497
|
+
|
|
498
|
+ Private Sub LogMessage(message As String)
|
|
499
|
+ ' 確保在 UI 線程上執行
|
|
500
|
+ If txtLog.InvokeRequired Then
|
|
501
|
+ txtLog.Invoke(New Action(Of String)(AddressOf LogMessage), message)
|
|
502
|
+ Return
|
|
503
|
+ End If
|
|
504
|
+
|
|
505
|
+ ' 添加時間戳並顯示消息
|
|
506
|
+ Dim logEntry As String = DateTime.Now.ToString("[HH:mm:ss] ") & message
|
|
507
|
+ txtLog.AppendText(logEntry & Environment.NewLine)
|
|
508
|
+
|
|
509
|
+ ' 滾動到底部
|
|
510
|
+ txtLog.SelectionStart = txtLog.Text.Length
|
|
511
|
+ txtLog.ScrollToCaret()
|
|
512
|
+ End Sub
|
|
513
|
+
|
|
514
|
+ Private Sub LogMessageWithDetails(message As String, ex As Exception)
|
|
515
|
+ LogMessage(message & ": " & ex.Message)
|
|
516
|
+
|
|
517
|
+ ' 添加詳細的異常信息,包括內部異常
|
|
518
|
+ If ex.InnerException IsNot Nothing Then
|
|
519
|
+ LogMessage("內部錯誤: " & ex.InnerException.Message)
|
|
520
|
+ End If
|
|
521
|
+
|
|
522
|
+ ' 添加堆棧跟踪
|
|
523
|
+ LogMessage("詳細信息: " & ex.ToString())
|
|
524
|
+ End Sub
|
|
525
|
+
|
|
526
|
+ Private Sub SaveDebugEmail(subject As String, body As String, recipients As String)
|
|
527
|
+ Try
|
|
528
|
+ ' 創建調試目錄
|
|
529
|
+ Dim debugDir As String = Path.Combine(Application.StartupPath, "debug_emails")
|
|
530
|
+ If Not Directory.Exists(debugDir) Then
|
|
531
|
+ Directory.CreateDirectory(debugDir)
|
|
532
|
+ End If
|
|
533
|
+
|
|
534
|
+ ' 創建唯一文件名
|
|
535
|
+ Dim filename As String = "email_" & DateTime.Now.ToString("yyyyMMdd_HHmmss") & ".txt"
|
|
536
|
+ Dim filepath As String = Path.Combine(debugDir, filename)
|
|
537
|
+
|
|
538
|
+ ' 保存郵件內容
|
|
539
|
+ Using writer As New StreamWriter(filepath, False, Encoding.UTF8)
|
|
540
|
+ writer.WriteLine("Date: " & DateTime.Now.ToString())
|
|
541
|
+ writer.WriteLine("From: " & txtFrom.Text)
|
|
542
|
+ writer.WriteLine("To: " & recipients)
|
|
543
|
+ writer.WriteLine("Subject: " & subject)
|
|
544
|
+ writer.WriteLine("-------------------------")
|
|
545
|
+ writer.WriteLine(body)
|
|
546
|
+ End Using
|
|
547
|
+
|
|
548
|
+ LogMessage("已保存調試郵件到: " & filepath)
|
|
549
|
+ Catch ex As Exception
|
|
550
|
+ LogMessage("保存調試郵件失敗: " & ex.Message)
|
|
551
|
+ End Try
|
|
552
|
+ End Sub
|
|
553
|
+End Class
|
|
554
|
+
|
|
555
|
+Module Program
|
|
556
|
+ Sub Main()
|
|
557
|
+ Application.EnableVisualStyles()
|
|
558
|
+ Application.SetCompatibleTextRenderingDefault(False)
|
|
559
|
+ Application.Run(New SmtpTestForm())
|
|
560
|
+ End Sub
|
|
561
|
+End Module
|