Function To Send Email - ASP.NET (C#)

User needs to pass few parameter to this method to send the mail.
body - Content of email that you want to send.
toadd - Address of the user to whom you want to send the Email.
ccAdd - Address of the user to whom you want to send Email in CC.
bccadd -Address of the user to whom you want to send Email in BCC.
fromaddm - Address of the user that want to send the Email.
subject - Subject of the Email.
attachment - Attachment any if you want to send to user otherwise set the value as blank if you don't want to send any email.

Address of SMTP Server - Replace this text with the address of your SMTP Server.
Code To Send Email:
public static bool SendMail(string body, string toadd, string fromadd, string subject, string attachment, string ccAdd, string bccadd)
{
string mailServerName = "Address of SMTP Server ";
try
{
//MailMessage represents the e-mail being sent
using (MailMessage message = new MailMessage(fromadd, toadd, subject, body))
{
if (attachment != "")
{
message.Attachments.Add(new Attachment(attachment));
}
if (bccadd != "")
{
message.Bcc.Add(bccadd);
}
message.IsBodyHtml = true;
message.Priority = MailPriority.Normal;
SmtpClient mailClient = new SmtpClient();
mailClient.Host = mailServerName;
mailClient.UseDefaultCredentials = true;
mailClient.Send(message);

}
return true;
}
catch (SmtpException ex)
{

return false;
}
catch (Exception ex)
{

return false;
}
}

0 comments: