-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMail.cs
165 lines (140 loc) · 6.35 KB
/
EMail.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace PaJaMa.Common
{
public static class EMail
{
public static bool Send(string host, int port, bool enableSsl, string username, string password, MailAddress sender, List<MailAddress> recipients, string subject,
string body, bool isBodyHtml, List<Attachment> attachments)
{
return Send(host, port, enableSsl, username, password, sender, recipients, null, subject, body, isBodyHtml, attachments);
}
public static bool Send(string host, int port, bool enableSsl, string username, string password, MailAddress sender, List<MailAddress> recipients, List<MailAddress> ccs,
string subject, string body, bool isBodyHtml, List<Attachment> attachments)
{
return Send(host, port, enableSsl, username, password, sender, recipients, ccs, null, subject, body, isBodyHtml, attachments);
}
public static bool Send(string host, int port, bool enableSsl, string username, string password, MailAddress sender, List<MailAddress> recipients, List<MailAddress> ccs,
List<MailAddress> bccs, string subject, string body, bool isBodyHtml, List<Attachment> attachments)
{
MailMessage msg = new MailMessage();
try
{
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.EnableSsl = enableSsl;
client.Host = host;
client.Port = port;
if (!string.IsNullOrEmpty(username))
client.Credentials = new System.Net.NetworkCredential(username, password);
msg.IsBodyHtml = isBodyHtml;
msg.From = sender;
foreach (MailAddress recipient in recipients)
{
msg.To.Add(recipient);
}
if (ccs != null)
{
foreach (MailAddress cc in ccs)
{
msg.CC.Add(cc);
}
}
if (bccs != null)
{
foreach (MailAddress bcc in bccs)
{
msg.Bcc.Add(bcc);
}
}
if (attachments != null && attachments.Count > 0)
attachments.ForEach(a => msg.Attachments.Add(a));
msg.Subject = subject;
msg.Body = body;
client.Send(msg);
}
catch (Exception ex)
{
EMailException emex = new EMailException(msg, ex);
Logging.LogException(emex);
throw emex;
}
return true;
}
public static bool Send(string host, string username, string password, MailAddress sender, List<MailAddress> recipients, string subject,
string body, bool isBodyHtml, List<Attachment> attachments)
{
return Send(host, 25, false, username, password, sender, recipients, subject, body, isBodyHtml, attachments);
}
public static bool Send(string host, string username, string password, MailAddress sender, List<MailAddress> recipients, List<MailAddress> ccs,
string subject, string body, bool isBodyHtml, List<Attachment> attachments)
{
return Send(host, 25, false, username, password, sender, recipients, ccs, subject, body, isBodyHtml, attachments);
}
public static bool Send(string host, string username, string password, MailAddress sender, List<MailAddress> recipients, string subject, string body, int port = 25, bool enableSsl = false,
bool isBodyHtml = true, List<Attachment> attachments = null, List<MailAddress> ccs = null, List<MailAddress> bccs = null)
{
return Send(host, port, enableSsl, username, password, sender, recipients, ccs, bccs, subject, body, isBodyHtml, attachments);
}
//public static bool Send(string host, int port, bool enableSsl, string username, string password, MailAddress sender, MailAddress recipient, string subject, string body, bool isBodyHtml)
//{
// return Send(host, port, enableSsl, username, password, sender, new List<MailAddress>() { recipient }, subject, body, isBodyHtml);
//}
//public static bool Send(string host, int port, bool enableSsl, string username, string password, MailAddress sender, MailAddress recipient, string subject, string body)
//{
// return Send(host, port, enableSsl, username, password, sender, new List<MailAddress>() { recipient }, subject, body, false);
//}
//public static bool Send(string host, int port, bool enableSsl, string username, string password, string sender, string recipient, string subject, string body, bool isBodyHtml)
//{
// return Send(host, port, enableSsl, username, password, new MailAddress(sender), new MailAddress(recipient), subject, body, isBodyHtml);
//}
//public static bool Send(string host, int port, bool enableSsl, string username, string password, string sender, string recipient, string subject, string body)
//{
// return Send(host, port, enableSsl, username, password, new MailAddress(sender), new MailAddress(recipient), subject, body, false);
//}
//public static bool Send(string host, int port, bool enableSsl, string username, string password, string subject, string body, bool isBodyHtml)
//{
// return Send(host, port, enableSsl, username, password, new MailAddress("[email protected]", "PJServer"), new MailAddress("[email protected]", "PJ Martins"), subject, body, isBodyHtml);
//}
//public static bool Send(MailAddress recipient, string subject, string body, bool isBodyHtml)
//{
//}
//public static bool Send(MailAddress recipient, string subject, string body)
//{
// return Send(recipient, subject, body, false);
//}
//public static bool Send(string recipient, string subject, string body, bool isBodyHtml)
//{
// return Send(new MailAddress(recipient), subject, body, isBodyHtml);
//}
//public static bool Send(string recipient, string subject, string body)
//{
// return Send(recipient, subject, body, false);
//}
//public static bool Send(string subject, string body, bool isBodyHtml)
//{
//}
//public static bool Send(string subject, string body)
//{
// return Send(subject, body, false);
//}
//public static bool SendException(Exception ex)
//{
// return Send("Exception thrown in " + System.Reflection.Assembly.GetCallingAssembly().FullName,
// "Exception thrown in " + System.Reflection.Assembly.GetCallingAssembly().FullName + "\r\n\r\n" +
// "Source: " + ex.Source + "\r\nException: " + Common.GetFullExceptionText(ex));
//}
}
public class EMailException : Exception
{
public MailMessage MailMessage { get; private set; }
public EMailException(MailMessage message, Exception exception)
: base(exception.Message, exception.InnerException)
{
MailMessage = message;
}
}
}