Created
July 11, 2014 13:09
-
-
Save CipherLab/a8672145e4e7c519bf21 to your computer and use it in GitHub Desktop.
Emailer Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Configuration; | |
using System.Collections; | |
//using System.Linq; | |
using System.Net.Mail; | |
using System.Net.Mime; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
using System.IO; | |
using System.Collections.Generic; | |
public class Emailer | |
{ | |
//public class EmailException : Exception | |
//{ | |
// public EmailException(string message) : base(message) { } | |
// public EmailException(string message, Exception innerException) : base(message, innerException) { } | |
//} | |
//public delegate void EmailMessageHandler(string msg); | |
//public event EmailMessageHandler EmailMessage; | |
public List<string> emailTo = new List<string>(); | |
public string emailFrom = ""; | |
public string emailSubject = ""; | |
public string emailBody = ""; | |
public SmtpClient SmtpCl = new SmtpClient(); | |
public int emailPort = 25; | |
public string[] attachments; | |
private void EmailMessage(string msg) | |
{ | |
} | |
public void SendMessage() | |
{ | |
try | |
{ | |
for (int i = 0; i < emailTo.Count; i++) | |
{ | |
emailTo[i] = emailTo[i].ToString().Replace("}", ""); | |
emailTo[i] = emailTo[i].ToString().Replace("{", ""); | |
emailTo[i] = emailTo[i].ToString().Replace('"', ' '); | |
emailTo[i] = emailTo[i].ToString().Trim(); | |
// validate the email address | |
bool bTest = ValidateEmailAddress(emailTo[i].ToString()); | |
// if the email address is bad, return message | |
if (bTest == false) | |
{ | |
EmailMessage("Invalid recipient email address: (" + emailTo[i] + ")"); | |
} | |
// create the email message | |
MailMessage message = new MailMessage(emailFrom, emailTo[i].ToString(), emailSubject, emailBody); | |
// add credentials | |
SmtpCl.Port = emailPort; | |
SmtpCl.UseDefaultCredentials = true; | |
// send message | |
SmtpCl.Send(message); | |
EmailMessage("Message sent to (" + emailTo[i].ToString() + ") at (" + DateTime.Now.ToString() + ")."); | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception(ex.Message.ToString()); | |
} | |
} | |
public void SendMessageWithAttachment() | |
{ | |
try | |
{ | |
// validate email address | |
for (int i = 0; i < emailTo.Count; i++) | |
{ | |
emailTo[i] = emailTo[i].ToString().Replace("}", ""); | |
emailTo[i] = emailTo[i].ToString().Replace("{", ""); | |
emailTo[i] = emailTo[i].ToString().Replace('"', ' '); | |
emailTo[i] = emailTo[i].ToString().Trim(); | |
bool bTest = ValidateEmailAddress(emailTo[i].ToString()); | |
if (bTest == false) | |
{ | |
EmailMessage("Invalid recipient email address: (" + emailTo[i] + ")"); | |
} | |
// Create the basic message | |
MailMessage message = new MailMessage(emailFrom, emailTo[i].ToString(), emailSubject, emailBody); | |
foreach (string attach in attachments) | |
{ | |
if (File.Exists(attach)) | |
{ | |
Attachment attached = new Attachment(attach); | |
message.Attachments.Add(attached); | |
} | |
} | |
// Add credentials | |
SmtpCl.Port = emailPort; | |
SmtpCl.UseDefaultCredentials = true; | |
// send message | |
SmtpCl.Send(message); | |
message.Attachments.Clear(); | |
message.Dispose(); | |
EmailMessage("Message sent to (" + emailTo[i].ToString() + ") at (" + DateTime.Now.ToString() + ")."); | |
} | |
//return "; | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception(ex.Message.ToString()); | |
} | |
} | |
public bool ValidateEmailAddress(string emailAddress) | |
{ | |
try | |
{ | |
string TextToValidate = emailAddress; | |
Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}"); | |
// test email address with expression | |
if (expression.IsMatch(TextToValidate)) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
catch (Exception) | |
{ | |
EmailMessage("Invalid Email address."); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment