/
MMDBMail.cs
92 lines (87 loc) · 3.13 KB
/
MMDBMail.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
namespace MMDB.Core
{
public class MMDBMail
{
public string ToAddress { get; set; }
public string FromAddress { get; set; }
public string Subject { get; set; }
public string MessageText { get; set; }
public string CC { get; set; }
public string BCC { get; set; }
public bool Encrypted { get; set; }
public bool EnableSSL { get; set; }
public string AuthenticationType { get; set; }
public int Port { get; set; }
public MMDBMail()
{
}
public MMDBMail(string To, string From, string SubjectText, string MessageTxt)
{
Encrypted = false;
Port = 25;
AuthenticationType = "Basic";
EnableSSL = false;
ToAddress = To;
FromAddress = From;
Subject = SubjectText;
MessageText = MessageTxt;
//Get values out of the config
}
public void SetServer()
{
if (!Encrypted)
{
_SMTPUsername = ConfigurationSettings.AppSettings["SMTPUsername"].ToString();
_SMTPPassword = ConfigurationSettings.AppSettings["SMTPPassword"].ToString();
_SMTPServer = ConfigurationSettings.AppSettings["SMTPServer"].ToString();
}
else
{
MMDB.Core.MMDBEncryption.CypherKey = "MMD8S0lution3";
_SMTPUsername = MMDBEncryption.Decrypt(ConfigurationSettings.AppSettings["SMTPUsername"].ToString(), true);
_SMTPPassword = MMDBEncryption.Decrypt(ConfigurationSettings.AppSettings["SMTPPassword"].ToString(), true);
_SMTPServer = MMDBEncryption.Decrypt(ConfigurationSettings.AppSettings["SMTPServer"].ToString(), true);
}
}
private string _SMTPUsername = "";
private string _SMTPPassword = "";
private string _SMTPServer = "";
public bool SendMail()
{
MailMessage message = new MailMessage(FromAddress, ToAddress, Subject, MessageText);
SetServer();
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = EnableSSL;
client.Port = Port;
if (Encrypted)
{
}
message.CC.Add(CC);
message.Bcc.Add(BCC);
client.Host = _SMTPServer;
client.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential(_SMTPUsername, _SMTPPassword);
credential.GetCredential(client.Host, Port, AuthenticationType);
client.Credentials = credential;
message.ReplyTo = message.From;
try
{
client.Send(message);
}
catch(Exception err)
{
MMDBExceptionHandler.HandleException(err);
throw(err);
}
return true;
}
}
}