-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailKitSmptRSR.cs
327 lines (296 loc) · 10.2 KB
/
MailKitSmptRSR.cs
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
using MimeKit;
using MailKit.Security;
namespace MailKitNetSmtp
{
public class MailKitSmptRSR
{
internal string errorText = "";
internal string senderMail = "", recipientMail = "";
internal string smtpMessage = "", smtpSubject = "", smtpServer = "", smtpUserName = "", smtpPassword = "", mailerName = "", smtpCharSet = "";
internal List<string> attachments;
internal bool readreceipt, html = false, useSSL = true;
internal int smtpPort = 465, smtpConnecttype = 1, smtpAuthmethod = 5;
// Connection Type
internal const int None = 0;
internal const int Auto = 1;
internal const int SslOnConnect = 2;
internal const int StartTls = 3;
internal const int StartTlsWhenAvailable = 4;
// Priority
internal const int NoPriority = 0;
internal const int LowPriority = 1;
internal const int NormalPriority = 2;
internal const int HighPriority = 3;
MailKit.Net.Smtp.SmtpClient smtp;
MimeMessage mail;
//Constructor
public MailKitSmptRSR()
{
attachments = new List<string>();
smtp = new MailKit.Net.Smtp.SmtpClient();
mail = new MimeMessage();
}
internal MailboxAddress emailName(string name, string mail)
{
if (String.IsNullOrEmpty(name))
{
return new MailboxAddress(mail, mail);
}
else
{
return new MailboxAddress(name, mail);
}
}
public int Send()
{
int result = 0;
result = SmtpConnect();
if (result == -1) { return result; }
result = SmtpSend();
if (result == -1) { return result; }
result = SmtpDisconnect();
return result;
}
public void SetMessage(string pbmessage)
{
SetMessage(pbmessage, false);
}
public void SetMessage(string pbmessage, bool pbHTML)
{
smtpMessage = pbmessage;
html = pbHTML;
}
public void SetRecipientEmail(string pbRecipientName, string pbRecipientMail)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbRecipientMail))
{
errorText = "To Mail cannot be null";
throw new ArgumentNullException(paramName: nameof(pbRecipientMail), message: errorText);
}
mail.To.Add(emailName(pbRecipientName, pbRecipientMail));
recipientMail = pbRecipientMail;
}
public void SetCCRecipientEmail(string pbCCrecipientName, string pbCCrecipientMail)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbCCrecipientMail))
{
errorText = "CC Mail cannot be null";
throw new ArgumentNullException(paramName: nameof(pbCCrecipientMail), message: errorText);
}
mail.Cc.Add(emailName(pbCCrecipientName, pbCCrecipientMail));
}
public void SetBCCRecipientEmail(string pbBCCrecipientName, string pbBCCrecipientMail)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbBCCrecipientMail))
{
errorText = "Bcc Mail cannot be null";
throw new ArgumentNullException(paramName: nameof(pbBCCrecipientMail), message: errorText);
}
mail.Bcc.Add(emailName(pbBCCrecipientName, pbBCCrecipientMail));
}
public void SetReplyToEmail(string pbReplytoName, string pbReplytoMail)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbReplytoMail))
{
errorText = "ReplyTo Mail cannot be null";
throw new ArgumentNullException(paramName: nameof(pbReplytoMail), message: errorText);
}
mail.ReplyTo.Add(emailName(pbReplytoName, pbReplytoMail));
}
public void SetSenderEmail(string pbSenderName, string pbSenderMail)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbSenderMail))
{
errorText = "From Mail cannot be null";
throw new ArgumentNullException(paramName: nameof(pbSenderMail), message: errorText);
}
mail.From.Add(emailName(pbSenderName, pbSenderMail));
senderMail = pbSenderMail;
}
public void SetSMTPServer(string pbsmtpserver)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbsmtpserver))
{
errorText = "Server cannot be null";
throw new ArgumentNullException(paramName: nameof(pbsmtpserver), message: errorText);
}
smtpServer = pbsmtpserver;
}
public void SetSubject(string pbsubject)
{
smtpSubject = pbsubject;
}
public void SetAttachment(string pbattachment)
{
attachments.Add(@pbattachment);
}
public void SetCharSet(string pbcharset)
{
smtpCharSet = pbcharset;
}
public void SetUsernamePassword(string pbusername, string pbpassword)
{
ResetErrorMessage();
if (String.IsNullOrEmpty(pbusername))
{
errorText = "User Name cannot be null";
throw new ArgumentNullException(paramName: nameof(pbusername), message: errorText);
}
smtpUserName = pbusername;
smtpPassword = pbpassword;
}
public void SetPort(int pbport)
{
smtpPort = pbport;
}
public void SetAuthMethod(int pbauthmethod)
{
smtpAuthmethod = pbauthmethod;
}
public void SetConnectionType(int pbconnecttype)
{
smtpConnecttype = pbconnecttype;
}
public string GetLastErrorMessage()
{
return errorText;
}
internal void ResetErrorMessage()
{
errorText = "";
}
public void SetMailerName(string pbmailername)
{
mailerName = pbmailername;
}
public void SetPriority(int pbpriority)
{
switch (pbpriority)
{
case LowPriority:
SetPriorityLow();
break;
case NormalPriority:
SetPriorityNormal();
break;
case HighPriority:
SetPriorityHigh();
break;
default:
SetPriorityNone();
break;
}
}
public void SetPriorityNone() { }
public void SetPriorityLow()
{
mail.Priority = MessagePriority.NonUrgent;
}
public void SetPriorityNormal()
{
mail.Priority = MessagePriority.Normal;
}
public void SetPriorityHigh()
{
mail.Priority = MessagePriority.Urgent;
}
public void SetReadReceiptRequested(bool pbreadreceipt)
{
readreceipt = pbreadreceipt;
}
public int SmtpConnect()
{
ResetErrorMessage();
try
{
//Read Requested
if (readreceipt == true) { mail.Headers[HeaderId.DispositionNotificationTo] = senderMail; }
//XMailer
if (mailerName != "") { mail.Headers[HeaderId.XMailer] = mailerName; }
switch (smtpConnecttype)
{
case Auto:
smtp.Connect(smtpServer, smtpPort, SecureSocketOptions.Auto);
break;
case SslOnConnect:
smtp.Connect(smtpServer, smtpPort, SecureSocketOptions.SslOnConnect);
break;
case StartTls:
smtp.Connect(smtpServer, smtpPort, SecureSocketOptions.StartTls);
break;
case StartTlsWhenAvailable:
smtp.Connect(smtpServer, smtpPort, SecureSocketOptions.StartTlsWhenAvailable);
break;
default:
smtp.Connect(smtpServer, smtpPort, SecureSocketOptions.None);
break;
}
smtp.Authenticate(smtpUserName, smtpPassword);
return 1;
}
catch (Exception ex)
{
errorText = "Connect Error " + ex.Message;
return -1;
}
}
public int SmtpSend()
{
ResetErrorMessage();
try
{
if (String.IsNullOrEmpty(senderMail) || String.IsNullOrEmpty(recipientMail))
{
errorText = "The sender and the recipient is required";
return -1;
}
//Asunto
mail.Subject = smtpSubject;
//Cuerpo del Mensaje
var builder = new BodyBuilder();
if (html == true)
{
builder.HtmlBody = smtpMessage;
}
else
{
builder.TextBody = smtpMessage;
}
//Adjuntos
foreach (var attachment in attachments)
{
builder.Attachments.Add(attachment);
}
mail.Body = builder.ToMessageBody();
//Envio el Maail
smtp.Send(mail);
return 1;
}
catch (Exception ex)
{
errorText = "Send Error " + ex.Message;
return -1;
}
}
public int SmtpDisconnect()
{
ResetErrorMessage();
try
{
smtp.Disconnect(true);
return 1;
}
catch (Exception ex)
{
errorText = "Disconnect Error " + ex.Message;
return -1;
}
}
}
}