-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmtpSendClass.cs
149 lines (121 loc) · 6.17 KB
/
SmtpSendClass.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace MHPEmailerLib {
public class EMLSmtpSendClass {
private string FromEmailName = "";
private string FromEmailAddress = "";
private bool UseSSL = false;
private int port = 25;
private string mailhost = "";
private string deliverymail = "";
private string accountname = "";
private string accountpassword = "";
private string MailSubject = "";
private string MailHtmlBody = "";
private string MailTextBody = "";
private string ReplytoEmailName = "";
private string ReplyToEmailAddress = "";
public EMLSmtpSendClass(MailerSettings settings) {
UseSSL = settings.Mailer_smtp_ssl;
port = settings.Mailer_smtp_port;
mailhost = settings.Mailer_host;
deliverymail = settings.Mailer_deliveryemail;
FromEmailName = settings.Mailer_fromname;
FromEmailAddress = settings.Mailer_fromemail;
ReplytoEmailName = settings.Mailer_replytoname;
ReplyToEmailAddress = settings.Mailer_replytoemail;
accountname = settings.Mailer_account;
accountpassword = settings.Mailer_password;
if(FromEmailName.Length == 0) {
FromEmailName = FromEmailAddress;
}
if(ReplytoEmailName.Length == 0) {
ReplytoEmailName = ReplyToEmailAddress;
}
}
public EMLSmtpSendClass(bool m_usessl, int m_port, string m_mailhost, string m_accountname, string m_accountpassword, string m_deliverymail, string m_FromEmailName, string m_FromEmailAddress, string m_ReplytoEmailName, string m_ReplyToEmailAddress) {
UseSSL = m_usessl;
port = m_port;
mailhost = m_mailhost;
deliverymail = m_deliverymail;
FromEmailName = m_FromEmailName;
FromEmailAddress = m_FromEmailAddress;
ReplytoEmailName = m_ReplytoEmailName;
ReplyToEmailAddress = m_ReplyToEmailAddress;
accountname = m_accountname;
accountpassword = m_accountpassword;
if(FromEmailName.Length == 0) {
FromEmailName = FromEmailAddress;
}
if(ReplytoEmailName.Length == 0) {
ReplytoEmailName = ReplyToEmailAddress;
}
}
public EMLReturnClass Send(List<EMLMailTarget> targets, string m_MailSubject, string m_MailHtmlBody, string m_MailTextBody, bool substitute) {
MailSubject = m_MailSubject;
MailHtmlBody = m_MailHtmlBody;
MailTextBody = m_MailTextBody;
EMLReturnClass outcome = new EMLReturnClass();
AlternateView av1 = null;
AlternateView av2 = null;
SmtpClient client = new SmtpClient();
client.Port = port;
client.Host = mailhost;
client.EnableSsl = UseSSL;
if(accountname.Length > 0) {
client.Credentials = new NetworkCredential(accountname, accountpassword);
} else {
client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
}
foreach(var x in targets) {
if(EMLHelpers.IsValidEmail(x.EmailAddress)) {
try {
if(substitute) {
MailHtmlBody = EMLHelpers.SubstituteFields(m_MailHtmlBody, x);
MailTextBody = EMLHelpers.SubstituteFields(m_MailTextBody, x);
MailSubject = EMLHelpers.SubstituteFields(m_MailSubject, x);
}
MailMessage mailMessage = new MailMessage(new MailAddress(FromEmailAddress, FromEmailName), new MailAddress(x.EmailAddress, x.Name));
mailMessage.Subject = MailSubject;
if(EMLHelpers.IsValidEmail(deliverymail)) {
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;
mailMessage.Headers.Add("Disposition-Notification-To", deliverymail);
}
if(MailHtmlBody.Length > 0) {
mailMessage.IsBodyHtml = true;
if(MailTextBody.Length > 0) {
av2 = AlternateView.CreateAlternateViewFromString(MailTextBody, null, MediaTypeNames.Text.Plain);
mailMessage.AlternateViews.Add(av2);
av1 = AlternateView.CreateAlternateViewFromString(MailHtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
mailMessage.AlternateViews.Add(av1);
} else {
av1 = AlternateView.CreateAlternateViewFromString(MailHtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
mailMessage.AlternateViews.Add(av1);
}
} else {
mailMessage.IsBodyHtml = false;
av2 = AlternateView.CreateAlternateViewFromString(MailTextBody, null, MediaTypeNames.Text.Plain);
mailMessage.AlternateViews.Add(av2);
}
if(EMLHelpers.IsValidEmail(ReplyToEmailAddress)) {
mailMessage.Headers.Add("Return-Path", ReplyToEmailAddress);
mailMessage.ReplyToList.Add(new MailAddress(ReplyToEmailAddress, ReplytoEmailName));
}
client.Send(mailMessage);
} catch(Exception ex) {
outcome.Errors.Add("Sending Error with " + x.Name + " " + ex.ToString());
}
} else {
outcome.Errors.Add("No Email address for " + x.Name);
}
}
return outcome;
}
}
}