[go: up one dir, main page]

Menu

[6396b6]: / Email.cs  Maximize  Restore  History

Download this file

161 lines (147 with data), 6.4 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
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
/***********************************************************************************
This file is part of Clock Alert.
Clock Alert is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as published by
the Free Software Foundation.
Clock Alert is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clock Alert. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace Clock_Alert
{
class Email : IEmail
{
/// <summary>
/// Sender email address.
/// </summary>
private string sender;
/// <summary>
/// Receiver email address.
/// </summary>
private string receiver;
/// <summary>
/// Subject of the message.
/// </summary>
private string subject;
/// <summary>
/// Body of the message.
/// </summary>
private string body;
/// <summary>
/// Name or IP address of the SMTP host.
/// </summary>
private string SMTPHost;
/// <summary>
/// User name for the SMTP transactions.
/// </summary>
private string userName;
/// <summary>
/// Password for the SMTP transactions.
/// </summary>
private string password;
/// <summary>
/// Port number to connect to SMTP host for transactions.
/// </summary>
private int port;
/// <summary>
/// Specify whether the SMTP client uses the Secure Socket Layer (SSL) to encrypt the connection.
/// </summary>
private bool enableSSL;
private SmtpClient mailClient;
private Progress _progress;
/// <summary>
/// Initializes the Email class instance
/// </summary>
public Email()
{
sender = string.Empty;
receiver = string.Empty;
subject = string.Empty;
body = string.Empty;
SMTPHost = string.Empty;
userName = string.Empty;
password = string.Empty;
port = 25;
enableSSL = false;
mailClient = null;
}
/// <summary>
/// Configures the SMTP client for E-Mail transactions.
/// </summary>
/// <param name="SMTPHostName">Name or IP address of the SMTP host.</param>
/// <param name="userName">User name for the SMTP transactions.</param>
/// <param name="password">Password for the SMTP transactions.</param>
/// <param name="enableSSL">pecify whether the SMTP client uses the Secure Socket Layer (SSL) to encrypt the connection.</param>
/// <param name="port">Port number to connect to SMTP host for transactions.</param>
public void configure(string SMTPHostName, string userName, string password, bool enableSSL, int port)
{
SMTPHost = SMTPHostName;
this.userName = userName;
this.password = password;
this.enableSSL = enableSSL;
this.port = port;
mailClient = new SmtpClient(SMTPHost);
mailClient.Port = this.port;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Credentials = new System.Net.NetworkCredential(this.userName, this.password);
mailClient.EnableSsl = this.enableSSL;
}
/// <summary>
/// Sets the E-mail fields required for sending an E-mail.
/// </summary>
/// <param name="fromAddress">Sender email address.</param>
/// <param name="toAddress">Receiver email address</param>
/// <param name="subject">Subject of the message</param>
/// <param name="body">Body of the message</param>
public void setFields(string fromAddress, string toAddress, string subject, string body)
{
sender = fromAddress;
receiver = toAddress;
this.subject = subject;
this.body = body;
}
/// <summary>
/// Send the email with the specified configuration and fields.
/// </summary>
public void sendMail()
{
if (mailClient != null)
{
MailMessage mail = new MailMessage(sender, receiver);
mail.IsBodyHtml = true;
mail.Subject = subject;
mail.Body = body;
//mailClient.Send(mail);
mailClient.SendCompleted += new SendCompletedEventHandler(mailClient_SendCompleted);
mailClient.SendAsync(mail, "Crash Report");
_progress = new Progress();
_progress.ShowDialog();
}
else
{
throw new Exception("Email server has not been configured. please call configure method first");
}
}
void mailClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
_progress.Close();
if (e.Error != null)
{
ErrorLog.logError(e.Error);
System.Windows.Forms.MessageBox.Show(Contents.crashReportErrorTitle, Contents.crashReportErrMsgP1 + '\"' + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + System.IO.Path.DirectorySeparatorChar + "Moon01Man" + System.IO.Path.DirectorySeparatorChar + "Clock Alert" + System.IO.Path.DirectorySeparatorChar + '\"' + Contents.crashReportErrMsgP2, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
_progress.Close();
System.Windows.Forms.MessageBox.Show(Contents.crashReportSentMessage, Contents.crashReportSentTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
}
}