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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 | /*
* TSSI .NET SMTP Component Demo (C# 2008)
*
* Note: to test your own SMTP account and the received eMails,
* you may modify the following code that starts with the "MBC" (may be changed) mark in the comment line.
*
* To read the detailed documentation, please visit
* https://www.automailsender.com/tssi-dotnet-smtp-component/help.htm
*
* © TriSun Software Inc. All rights reserved.
*/
// Imports TDNSC's namespace first.
using TSSI.eMail;
// Some native members of this namespace will be used in your code.
using System.Net.Mail;
// The third-party open source proxy library.
using Starksoft.Net.Proxy;
// Just for the attachments.
using System.IO;
using Microsoft.VisualBasic;
using System.Windows.Forms;
using System;
namespace TDNSC_Demo_CS_2008 {
public partial class Main : Form {
// TSMTP instance (used to send eMail via the (E)SMTP protocol).
private TSMTP _tsmtp = new TSMTP();
// TMailMessage instance (the eMail for sending).
private TMailMessage _tmm = new TMailMessage();
public Main() {
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e) {
//// Adds the event handles of _tsmtp.
_tsmtp.ErrorOccurred +=new TSMTP.ErrorOccurredEventHandler(_tsmtp_ErrorOccurred);
_tsmtp.Progress +=new TSMTP.ProgressEventHandler(_tsmtp_Progress);
////
////// Set the connection and authentication information of your TSMTP instance.
//// MBC: Set the proxy server information if necessary.
// The host name or IP address of the proxy server.
// _tsmtp.ProxyHost = "192.168.1.100";
// Connection port.
// _tsmtp.ProxyPort = 1080;
// Proxy protocol, currently only supports Socks5 - the common SMTP proxy protocol.
// _tsmtp.ProxyProtocol = ProxyType.Socks5;
// User name.
// _tsmtp.ProxyUser = "myproxyacc";
// Password.
// _tsmtp.ProxyPasswd = "myproxypwd";
////
//// Set the SMTP account information.
// MBC: SMTP server.
_tsmtp.SMTPServer = "smtp.gmail.com";
// MBC: SMTP port.
_tsmtp.SMTPPort = 465;
// MBC: SMTP account for authentication required SMTP server (most SMTP servers need this).
_tsmtp.SMTPUser = "[email protected]";
// MBC: SMTP password for authentication required SMTP server (most SMTP servers need this).
_tsmtp.SMTPPasswd = "mypasswd";
// MBC: Security connection protocol, if you are not sure, please contact the SMTP provider.
// It can be SecureProtocol.None and SecureProtocol.TLS also.
_tsmtp.Protocol = SecureProtocol.SSL;
////
//////
}
private void _tsmtp_ErrorOccurred(string desc) {
/* Display the error messages during sending. */
tbErr.Text = tbErr.Text + "\r\n\r\n" + desc;
}
private void _tsmtp_Progress(int id, string desc) {
/* Display the progress during sending. */
tbProgress.Text = tbProgress.Text + "\r\n\r\n" + id + " - " + desc;
}
private void ComposeEmail() {
/* Compose your message. */
// Subject.
_tmm.Subject = "A Test Message Sent by TDNSC";
// Not a HTML (rich text) email body (default).
_tmm.IsBodyHtml = false;
// The eMail content (plain text).
_tmm.Body = "Hi, this is a test message that sent by TSSI .NET SMTP Component.";
//// Add the attachments, if there is no attachment, please do not use this property.
_tmm.Attachments.Clear();
_tmm.Attachments.Add(new FileInfo(new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName +
"\\atts\\testatt.txt"), null, null, null);
_tmm.Attachments.Add(new FileInfo(new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName +
"\\atts\\testatt.png"), null, null, null);
// .Attachments.Add // Add more...
////
// MBC: Sender's eMail address.
_tmm.Sender = new MailAddress("[email protected]");
// MBC: This value will displayed in message header (From...) of the received email.
_tmm.From = new MailAddress("[email protected]", "From");
// MBC: Sets the ReplyTo address for the mail message.
_tmm.ReplyTo = new MailAddress("[email protected]", "ReplyTo");
// Whether need a read receipt or not, if do not need, please leave it blank.
_tmm.Receipt = true;
//// MBC: If _tmm.Receipt property is set as true,
//// you can specify a different address than _tmm.From to receive the receipt.
//// You can also leave _tmm.ReceiptReceiver as blank to use _tmm.From address
//// to receive the receipt.
_tmm.ReceiptReceiver = new MailAddress("[email protected]", "RR");
////
// Make the virtual eMailing time if necessary. Format: DDD, d MMM YYYY HH:MI:SS.
// _tmm.SendTime = "Fri, 4 Jul 2014 14:03:08";
//// Specifies the priority level for the e-mail message.
//// Please uncomment one if necessary.
// _tmm.Priority = MailPriority.High;
// _tmm.Priority = MailPriority.Low;
// Normal is the default value, if you have not priority requirement,
// please leave it blank.
// _tmm.Priority = MailPriority.Normal;
////
////// Add the receivers.
//// MBC: [To].
_tmm.To.Clear();
_tmm.To.Add(new MailAddress("[email protected]", "You@Gmail"));
_tmm.To.Add(new MailAddress("[email protected]", "You@Hotmail"));
// _tmm.To.Add... // Add more...
////
//// MBC: [CC].
_tmm.CC.Clear();
_tmm.CC.Add(new MailAddress("[email protected]", "Him@Gmail"));
_tmm.CC.Add(new MailAddress("[email protected]", "Him@Hotmail"));
// _tmm.CC.Add... // Add more...
////
//// MBC: [BCC].
_tmm.Bcc.Clear();
_tmm.Bcc.Add(new MailAddress("[email protected]", "Her@Gmail"));
_tmm.Bcc.Add(new MailAddress("[email protected]", "Her@Hotmail")); ;
//// _tmm.Bcc.Add... // Add more...
//////
}
private void bTestLogon_Click(object sender, EventArgs e) {
/* Test if the given user and password can be used to log on the SMTP server or not. */
// Initialize the error messages box.
tbErr.Text = "Error Messages";
// Initialize the progress box.
tbProgress.Text = "Progress";
//// Set the interface to be busy.
Cursor.Current = Cursors.WaitCursor;
this.Enabled = false;
////
try {
if (_tsmtp.Connect(_tsmtp.SMTPServer, _tsmtp.SMTPPort)) {
// This utility method is used to test if the given user and
// password can be used to log on the SMTP server or not.
if (_tsmtp.TestLogon()) {
Interaction.MsgBox("Connected and logged in to the SMTP server successfully.",
MsgBoxStyle.Information,null);
}
else {
Interaction.MsgBox("Cannot logged on the SMTP server according to your given information.",
MsgBoxStyle.Exclamation,null);
}
}
else {
Interaction.MsgBox("Cannot connect to the SMTP server according" +
" to your given information in your LAN.",
MsgBoxStyle.Exclamation,null);
}
} catch (Exception ex) {
// Display the exception also.
tbErr.Text = tbErr.Text + Constants.vbCrLf + ex.Message;
}
// Always disconnect from the SMTP server.
_tsmtp.Disconnect();
//// Set the interface to be normal.
Cursor.Current = Cursors.Default;
this.Enabled = true;
////
}
private void bAbout_Click(object sender, EventArgs e) {
/* Displays the About box, it contains the program and edition information. */
TSMTP.About();
}
private void bCheck_Click(object sender, EventArgs e) {
/*
Check the format of an eMail address.
TMailMessage.CheckAddress() is a shared method, just call it from the class.
*/
// The valid one.
Interaction.MsgBox("[email protected] is " +
(TMailMessage.CheckAddress("[email protected]") ? "valid!" : "invalid!"),
MsgBoxStyle.Information,null);
// The invalid one.
Interaction.MsgBox("[email protected] is " +
(TMailMessage.CheckAddress("[email protected]") ? "valid!" : "invalid!"),
MsgBoxStyle.Information,null);
}
private void bPlain_Click(object sender, EventArgs e) {
/* Send a plain text message. */
// Compose your message.
ComposeEmail();
////// Send message.
// Initialize the error messages box.
tbErr.Text = "Error Messages";
// Initialize the progress box.
tbProgress.Text = "Progress";
//// Set the interface to be busy.
Cursor.Current = Cursors.WaitCursor;
this.Enabled = false;
////
try {
if (_tsmtp.Connect(_tsmtp.SMTPServer, _tsmtp.SMTPPort)) {
if (_tsmtp.SendMsg(_tmm)) {
Interaction.MsgBox("Sent the plain text message successfully!",
MsgBoxStyle.Information,null);
}
else {
Interaction.MsgBox("Failed to send the plain text eMail, please" +
" see the error messages to know about the reason.",
MsgBoxStyle.Information,null);
}
}
} catch (Exception ex) {
// Display the exception also.
tbErr.Text = tbErr.Text + Constants.vbCrLf + ex.Message;
}
// Always disconnect from the SMTP server.
_tsmtp.Disconnect();
//// Set the interface to be normal.
Cursor.Current = Cursors.Default;
this.Enabled = true;
////
//////
}
private void bRich_Click(object sender, System.EventArgs e) {
/* Send a rich text message. */
//// Compose your message.
ComposeEmail();
// Set as HTML (rich text) email body.
_tmm.IsBodyHtml = true;
// The eMail content (plain text).
_tmm.Body = "<html><body><h2>Hi, this is a test message that sent by " +
"TSSI .NET SMTP Component.</h2><img src=\"" +
new DirectoryInfo(Application.StartupPath).Parent.Parent.FullName +
"\\atts\\testatt.png\" /></body></html>";
////
////// Send message.
// Initialize the error messages box.
tbErr.Text = "Error Messages";
// Initialize the progress box.
tbProgress.Text = "Progress";
//// Set the interface to be busy.
Cursor.Current = Cursors.WaitCursor;
this.Enabled = false;
////
try {
if (_tsmtp.Connect(_tsmtp.SMTPServer, _tsmtp.SMTPPort)) {
if (_tsmtp.SendMsg(_tmm)) {
Interaction.MsgBox("Sent the rich text message successfully!",
MsgBoxStyle.Information, null);
}
else {
Interaction.MsgBox("Failed to send the rich text eMail, " +
"please see the error messages to know about the reason.",
MsgBoxStyle.Information, null);
}
}
} catch (Exception ex) {
// Display the exception also.
tbErr.Text = tbErr.Text + Constants.vbCrLf + ex.Message;
}
// Always disconnect from the SMTP server.
_tsmtp.Disconnect();
//// Set the interface to be normal.
Cursor.Current = Cursors.Default;
this.Enabled = true;
////
//////
}
}
}
|