-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
117 lines (105 loc) · 3.1 KB
/
Form1.cs
File metadata and controls
117 lines (105 loc) · 3.1 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace SmtpClientTest
{
/// <summary>
/// SmtpClientTest
/// </summary>
/// <history>
/// 2007/04/02, lozen_lin, Create
/// 2012/12/03, lozen_lin, Modify, add sender
/// 2013/01/30, lozen_lin, Modify, show detail error message; show version number; 讓 EnableSsl 可以手動設定;
/// </history>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Text += " - v" + Application.ProductVersion;
}
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage MailMsg = null;
try
{
SmtpClient MailSender = new SmtpClient(cboSmtpServer.Text, Convert.ToInt32(nudPort.Value));
MailSender.EnableSsl = chkEnableSsl.Checked;
MailSender.Timeout = 60000;
if (chkNeedAccount.Checked)
{
//要帳號
MailSender.UseDefaultCredentials = false;
MailSender.Credentials =
new System.Net.NetworkCredential(txtAccount.Text, txtPassword.Text);
}
else
{
//用預設
MailSender.UseDefaultCredentials = true;
}
MailMsg = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
MailMsg.IsBodyHtml = chkIsBodyHtml.Checked; // 2020/01/22, lozenlin, add
//註記真正的寄件人
if (!string.IsNullOrEmpty(txtSender.Text.Trim()))
{
MailMsg.Sender = new MailAddress(txtSender.Text);
}
//附件
if (lstAttachments.Items.Count > 0)
{
foreach (string FileName in lstAttachments.Items)
MailMsg.Attachments.Add(new Attachment(FileName));
}
MailSender.Send(MailMsg);
MessageBox.Show("寄送成功!", "提示");
}
catch (SmtpException smtpex)
{
string errMsg = smtpex.Message;
if (smtpex.InnerException != null)
{
errMsg += "-" + smtpex.InnerException.Message;
}
MessageBox.Show(errMsg + "\nStatusCode: " + smtpex.StatusCode.ToString(), "Smtp例外錯誤");
}
catch (Exception ex)
{
string errMsg = ex.Message;
if (ex.InnerException != null)
{
errMsg += "-" + ex.InnerException.Message;
}
MessageBox.Show(ex.Message, "例外錯誤");
}
finally
{
if (MailMsg != null)
MailMsg.Dispose();
//清除附件清單
lstAttachments.Items.Clear();
}
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
//開檔
DialogResult dlgResult = openFileDialog1.ShowDialog();
if (dlgResult == DialogResult.OK)
{
//加入附件清單
lstAttachments.Items.Add(openFileDialog1.FileName);
}
}
private void btnDel_Click(object sender, EventArgs e)
{
//刪除附件清單項目
if (lstAttachments.SelectedIndex != -1)
lstAttachments.Items.RemoveAt(lstAttachments.SelectedIndex);
}
}
}