Email sending in C#
Email sending with Attachment
public static bool SendEmailMessage(string SenderEmailAddress, string SenderPassword, string SenderHostName, int SenderPort, string Subject, string Body, string emailTO, bool EnableSSL = false, string CC = "", string BCC = "", MailPriority Priority = MailPriority.Normal, string path = "")
{
try
{
System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
//Set from address
objMail.From = new MailAddress(SenderEmailAddress);
// Set to addresses
if (string.IsNullOrEmpty(emailTO))
{
objMail.To.Add(new MailAddress(emailTO));
}
// Set CC addresses
if (string.IsNullOrEmpty(CC))
{
objMail.CC.Add(new MailAddress(CC));
}
// Set BCC addresses
if (string.IsNullOrEmpty(BCC))
{
objMail.Bcc.Add(new MailAddress(BCC));
}
// Set subject
objMail.Subject = Subject;
// Set body/message
objMail.Body = Body;
objMail.IsBodyHtml = true;
// Set priority default is "Normal"
objMail.Priority = Priority;
// Set attachement
if (string.IsNullOrEmpty(path))
{
objMail.Attachments.Add(new Attachment(path));
}
SmtpClient SMTPClientObj = new SmtpClient();
SMTPClientObj.UseDefaultCredentials = false;
SMTPClientObj.Credentials = new System.Net.NetworkCredential(SenderEmailAddress, SenderPassword); //Credentials of sender
SMTPClientObj.Host = SenderHostName; //Host name
SMTPClientObj.DeliveryMethod = SmtpDeliveryMethod.Network;
SMTPClientObj.Port = SenderPort; // Port Number
SMTPClientObj.EnableSsl = EnableSSL;
SMTPClientObj.Send(objMail);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static bool SendEmailMessage(string SenderEmailAddress, string SenderPassword, string SenderHostName, int SenderPort, string Subject, string Body, string emailTO, bool EnableSSL = false, string CC = "", string BCC = "", MailPriority Priority = MailPriority.Normal, string path = "")
{
try
{
System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
//Set from address
objMail.From = new MailAddress(SenderEmailAddress);
// Set to addresses
if (string.IsNullOrEmpty(emailTO))
{
objMail.To.Add(new MailAddress(emailTO));
}
// Set CC addresses
if (string.IsNullOrEmpty(CC))
{
objMail.CC.Add(new MailAddress(CC));
}
// Set BCC addresses
if (string.IsNullOrEmpty(BCC))
{
objMail.Bcc.Add(new MailAddress(BCC));
}
// Set subject
objMail.Subject = Subject;
// Set body/message
objMail.Body = Body;
objMail.IsBodyHtml = true;
// Set priority default is "Normal"
objMail.Priority = Priority;
// Set attachement
if (string.IsNullOrEmpty(path))
{
objMail.Attachments.Add(new Attachment(path));
}
SmtpClient SMTPClientObj = new SmtpClient();
SMTPClientObj.UseDefaultCredentials = false;
SMTPClientObj.Credentials = new System.Net.NetworkCredential(SenderEmailAddress, SenderPassword); //Credentials of sender
SMTPClientObj.Host = SenderHostName; //Host name
SMTPClientObj.DeliveryMethod = SmtpDeliveryMethod.Network;
SMTPClientObj.Port = SenderPort; // Port Number
SMTPClientObj.EnableSsl = EnableSSL;
SMTPClientObj.Send(objMail);
return true;
}
catch (Exception ex)
{
return false;
}
}
Comments
Post a Comment