-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCsFoxySmtp.cs
274 lines (234 loc) · 8.01 KB
/
CsFoxySmtp.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
using System;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CsFoxySmtp
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("CsFoxySmtp")]
/// <summary>
/// DLL en C# para Enviar Correo desde VFP.
/// Microsoft: Outlook y Office365
/// Google: Gmail
/// </summary>
public class CsFoxySmtp : System.EnterpriseServices.ServicedComponent
{
private readonly string _version = "1.0.5";
public string from = "";
public string replayTo = "";
public string user = "";
public string password = "";
public string subjet = "";
public string body = "";
public bool bodyHtml = true;
public bool clean = false;
public bool fileSize = false;
public bool notification = false;
private Attachment fileAdd;
private readonly List<string> _emailTo = new List<string>();
private readonly List<string> _emailCc = new List<string>();
private readonly List<string> _emailBcc = new List<string>();
private readonly List<string> _filesList = new List<string>();
public string server = "";
public Int32 port = 587;
public bool ssl = true;
public int priority = 0;
private MailMessage mailMessage = new MailMessage();
public string Version => _version;
public string Error { get; private set; } = "";
public decimal Size { get; private set; } = 0;
public void Clear()
{
from = "";
replayTo = "";
subjet = "";
body = "";
Error = "";
Size = 0;
ClearTo();
ClearCc();
ClearBcc();
ClearAttachments();
}
private MailPriority Priority(Int32 num)
{
switch (num)
{
case 0:
return MailPriority.Normal;
case 1:
return MailPriority.Low;
case 2:
return MailPriority.High;
default:
return MailPriority.Normal;
}
}
public Boolean Smtp()
{
bool send = false;
Error = "";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls |
SecurityProtocolType.Ssl3;
mailMessage = new MailMessage(
from: from,
to: To,
subject: subjet,
body: null
)
{
IsBodyHtml = bodyHtml,
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8,
Priority = Priority(priority),
From = new MailAddress(from),
Sender = new MailAddress(from)
};
if (HtmlView == null)
{
HtmlView = AlternateView.CreateAlternateViewFromString(
body,
Encoding.UTF8,
MediaTypeNames.Text.Html);
}
mailMessage.AlternateViews.Add(HtmlView);
/// ReplyToList
if (replayTo.Length > 0)
{
mailMessage.ReplyToList.Add(replayTo);
}
if ( notification == true )
{
mailMessage.Headers.Add( "Disposition-Notification-To", user );
mailMessage.Headers.Add( "Return-Receipt-To", user );
}
if (_emailCc.Count > 0)
{
_emailCc.ForEach(delegate (string email)
{
mailMessage.CC.Add(email);
});
};
if (_emailBcc.Count > 0)
{
_emailBcc.ForEach(delegate (string email)
{
mailMessage.Bcc.Add(email);
});
};
if (_filesList.Count > 0)
{
_filesList.ForEach(delegate (string file)
{
fileAdd = new Attachment(file, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(fileAdd);
});
};
SmtpClient smtpClient = new SmtpClient(server)
{
EnableSsl = ssl,
Host = server,
Port = port,
Credentials = new System.Net.NetworkCredential(user, password),
//TargetName = "STARTTLS/smtp.office365.com",
//UseDefaultCredentials = false,
//DeliveryMethod = SmtpDeliveryMethod.Network,
};
try
{
smtpClient.Send(mailMessage);
if ( clean == true )
{
Clear();
}
}
catch (Exception ex)
{
Error = ex.ToString();
}
smtpClient.Dispose();
if (_filesList.Count > 0)
{
fileAdd.Dispose();
}
HtmlView.Dispose();
HtmlView = null;
mailMessage.Attachments.Clear();
if ( Error.Length <= 0 )
{
send = true;
}
return send;
}
public string Attachments => ListToString(_filesList);
public decimal AddAttachments(string pathFile)
{
_filesList.Add(pathFile);
if ( fileSize == true )
{
decimal size = FileSize(pathFile);
Size += size;
} else
{
Size = 0;
}
return Size;
}
public void ClearAttachments() => _filesList.Clear();
public int CountAttachments => _filesList.Count;
public string Cc => ListToString(_emailCc);
public void AddCc(string email) => _emailCc.Add(email);
public void ClearCc() => _emailCc.Clear();
public int CountCc => _emailCc.Count;
public string Bcc => ListToString(_emailBcc);
public void AddBcc(string email) => _emailBcc.Add(email);
public void ClearBcc() => _emailBcc.Clear();
public int CountBcc => _emailBcc.Count;
public string To => ListToString(_emailTo);
public void AddTo(string email) => _emailTo.Add(email);
public void ClearTo() => _emailTo.Clear();
public int CountTo => _emailTo.Count;
public AlternateView HtmlView { get; set; }
private string ListToString(List<string> list)
{
string listToString = "";
int index = 0;
list.ForEach(delegate (string item)
{
index += 1;
if (index > 1)
{
listToString += ", ";
}
listToString += item;
});
return listToString;
}
private decimal FileSize(string pathFile)
{
FileInfo file = new FileInfo(pathFile);
return (file.Length / 1024);
}
public void EmbedImage( string file, string contentId )
{
if ( HtmlView == null )
{
HtmlView = AlternateView.CreateAlternateViewFromString(
body,
Encoding.UTF8,
MediaTypeNames.Text.Html);
}
LinkedResource img = new LinkedResource( @file, MediaTypeNames.Image.Jpeg )
{
ContentId = contentId
};
HtmlView.LinkedResources.Add(img);
}
}
}