Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public bool SendEmail(string from, string to, string subject, string message, string cc = null, string bcc = null, bool isHTML = false, List<Attachment> attachments = null, string fromAddressDisplayName = "", string sendingApplication = "Email Service Application")
{
bool isSent = false;
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
//MailMessage emailMessage = new MailMessage(from, to);
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress(from, fromAddressDisplayName);
// to address, it can be more than one
if (to != null)
{
List<string> toList = to.Trim().Split(';', ',').ToList();
foreach (string toAddress in toList)
{
if (!string.IsNullOrWhiteSpace(toAddress))
emailMessage.To.Add(new MailAddress(toAddress));
}
}
if (emailMessage.To.Count <= 0)
{
// there is no to email address specified
return isSent;
}
emailMessage.Subject = subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = isHTML;
// adding attachments
foreach (Attachment attachment in attachments ?? new List<Attachment>())
{
emailMessage.Attachments.Add(attachment);
}
// add cc list
if (cc != null)
{
List<string> ccList = cc.Trim().Split(';', ',').ToList();
foreach (string ccAddress in ccList)
{
if (!string.IsNullOrWhiteSpace(ccAddress))
emailMessage.CC.Add(ccAddress.Trim());
}
}
// add bcc list
if (bcc != null)
{
List<string> bccList = bcc.Trim().Split(';', ',').ToList();
foreach (string bccAddress in bccList)
{
if (!string.IsNullOrWhiteSpace(bccAddress))
emailMessage.Bcc.Add(bccAddress.Trim());
}
}
client.Send(emailMessage);
isSent = true;
}
catch (SmtpException smtpEx)
{
//DDAppLogger.Add("EmailService", "SendEmail", "", string.Format("SMTP error for email: {0}, Error message: {1}", to.Address, smtpEx.ToString()), -1, "SYSTEM", "UNKNOWN", true);
// if failed to email, write an entry in system event log
string log = "Error in Sending Email";
string sEvent = string.Format("Unable to send email. Subject : {0}, To Address : {1}. Details of Error: {2}", subject, to, smtpEx.ToString());
RegisterEventLog(sEvent, log, EventLogEntryType.Error, sendingApplication);
//throw smtpEx;
}
catch (Exception ex)
{
//DDAppLogger.Add("EmailService", "SendEmail", "", string.Format("Error for email: {0}, Error message: {1}", to.Address, ex.ToString()), -1, "SYSTEM", "UNKNOWN", true);
// if failed to email, write an entry in system event log
string log = "Error in Sending Email";
string sEvent = string.Format("Unable to send email. Subject : {0}, To Address : {1}. Details of Error: {2}\n\n Email Message: {3}", subject, to, ex.ToString(), message);
RegisterEventLog(sEvent, log, EventLogEntryType.Error, sendingApplication);
//throw ex;
}
return isSent;
}
public void RegisterEventLog(string strEvent, string log, EventLogEntryType logType, string source = "Email Service Application")
{
// Application source in system event log
try
{
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog.WriteEntry(source, strEvent, logType);
}
catch (Exception ex)
{
//DDAppLogger.Add("EmailService", "RegisterEventLog", "RegisterEventLog", ex.ToString(), -1, "UNKNOWN", "SYSTEM", true);
//throw ex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.