forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutgoingEmailDialog.cs
126 lines (109 loc) · 3.97 KB
/
OutgoingEmailDialog.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
using SAAI.Properties;
using System;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;
namespace SAAI
{
/// <summary>
/// In order to send email we need the outgoing email server address.
/// This dialog gets that information.
/// </summary>
public partial class OutgoingEmailDialog : Form
{
public OutgoingEmailDialog()
{
InitializeComponent();
serverText.Text = Settings.Default.EmailServer;
userText.Text = Settings.Default.EmailUser;
passwordText.Text = Settings.Default.EmailPassword;
sslCheck.Checked = Settings.Default.EmailSSL;
portNumeric.Value = Settings.Default.EmailPort;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(serverText.Text))
{
MessageBox.Show("You must fill out the email server, the user name, and the password.");
DialogResult = DialogResult.None;
}
else if (!userText.Text.Contains("@"))
{
MessageBox.Show(this, "You must include '@' plus the domain in your email user name. For example: '[email protected]'", "Email Sender Format Error");
DialogResult = DialogResult.None;
}
else
{
Settings.Default.EmailServer = serverText.Text;
Settings.Default.EmailUser = userText.Text;
Settings.Default.EmailPassword = passwordText.Text;
Settings.Default.EmailSSL = sslCheck.Checked;
Settings.Default.EmailPort = (uint)portNumeric.Value;
Settings.Default.EmailSetup = true;
Settings.Default.AISetup = true;
Settings.Default.Save();
DialogResult = DialogResult.OK;
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void TestButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(serverText.Text))
{
MessageBox.Show(this, "You must enter a a server address before testing!", "Server Empty!");
return;
}
if (!userText.Text.Contains("@"))
{
MessageBox.Show(this, "You must include '@' plus the domain in your email user name. For example: '[email protected]'" , "Email Sender Format Error");
return;
}
DialogResult destinationResult;
string destination = string.Empty;
using (TestEmailAddress dlg = new TestEmailAddress())
{
destinationResult = dlg.ShowDialog();
if (destinationResult == DialogResult.OK)
{
destination = dlg.emailAddressText.Text;
}
}
if (!string.IsNullOrEmpty(destination))
{
try
{
using (MailMessage mail = new MailMessage())
{
using (SmtpClient SmtpServer = new SmtpClient(serverText.Text))
{
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = true;
mail.From = new MailAddress(userText.Text);
mail.To.Add(destination);
mail.Subject = "Security Camera Test"; // todo get via ui
mail.Body = "This is a test of your email server settings<br />";
SmtpServer.Port = (int)portNumeric.Value;
SmtpServer.Credentials = new System.Net.NetworkCredential(userText.Text, passwordText.Text);
SmtpServer.EnableSsl = sslCheck.Checked ;
SmtpServer.Send(mail);
}
}
}
catch (SmtpException ex)
{
MessageBox.Show("There was an error sending a test email to your email address", "Email Error!");
Dbg.Write("Email exception: " + ex.ToString());
return;
}
MessageBox.Show("Your test email was sent successfully!");
}
else
{
MessageBox.Show("You must enter a destination email address in order to run this test", "Error!");
}
}
}
}