EmailUtility.cs
3.6 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.Text;
using System.IO;
using System.Net.Mail;
namespace AIAHTML5.API.Utility
{
public class EmailUtility
{
public string sFromAddress { get; set; }
public List<string> sToAddresses { get; set; }
public List<string> sBccAddresses { get; set; }
public string sHostName { get; set; }
public string sSubject { get; set; }
public int iPort { get; set; }
public bool bEnableSsl { get; set; }
public string sUserName { get; set; }
public string sPassword { get; set; }
public bool bIsBodyHtml { get; set; }
public List<Attachment> sAttachments { get; set; }
public string sBodyText { get; set; }
public AlternateView sAlternateView { get; set; }
public void SendMail(MailMessage mm)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTPAddress"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(mm.From.ToString(), ConfigurationManager.AppSettings["SenderPassword"]);
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
smtp.Send(mm);
}
public void SendSmtpEmail()
{
try
{
MailMessage sMail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(sHostName);
string recipientEmailAddress = string.Empty;
if (sToAddresses != null)
{
foreach (var sItem in sToAddresses)
{
sMail.To.Add(sItem);
}
}
if (sBccAddresses != null)
{
foreach (var sItem in sBccAddresses)
{
sMail.Bcc.Add(sItem);
}
}
sMail.IsBodyHtml = bIsBodyHtml;
if (sAlternateView != null)
{
sMail.AlternateViews.Add(sAlternateView);
}
else
{
sMail.Body = sBodyText;
}
sMail.Subject = sSubject;
if (sAttachments != null)
{
foreach (var sItem in sAttachments)
{
sMail.Attachments.Add(sItem);
}
}
SmtpServer.Port = iPort;
SmtpServer.Credentials = new System.Net.NetworkCredential(sUserName, sPassword);
SmtpServer.EnableSsl = bEnableSsl;
using (MailMessage mm = new MailMessage(sFromAddress, sMail.To.ToString()))
{
mm.Subject = sSubject;
mm.IsBodyHtml = bIsBodyHtml;
if (sAlternateView != null)
{
mm.AlternateViews.Add(sAlternateView);
}
else
{
mm.Body = sBodyText;
}
mm.IsBodyHtml = true;
SendMail(mm);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}