Utility Class to Send email

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Net.Mail;

namespace Utility
{
public class Mail
{
public static bool SendMail(string ToEmailID, string Subject,string FromEmailID, string FromDisplayName, string Body)
{
bool IsSent = false;
try
{
if (!string.IsNullOrEmpty(FromEmailID))
{
System.Net.Mail.MailMessage objEmail = new System.Net.Mail.MailMessage();
objEmail.To.Add(ToEmailID);
objEmail.From = new System.Net.Mail.MailAddress(FromEmailID, FromDisplayName);
objEmail.Subject = Subject;
objEmail.Body = Body;
objEmail.IsBodyHtml = true;
objEmail.BodyEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient myClient = new System.Net.Mail.SmtpClient();//”207.97.202.77″, 25
myClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;
myClient.Send(objEmail);
IsSent = true;
}
return IsSent;
}
catch (InvalidOperationException)
{
return IsSent;
}
catch (SmtpFailedRecipientException)
{
return IsSent;
}
catch (SmtpException)
{
return IsSent;
}
}

public static bool EmailReportWithAttachment(string emTo, string emFrom,string stringemSubject, string emBody, string emAttach)
{
bool IsSent = false;
MailAddress from = new MailAddress(emFrom);
MailAddress to = new MailAddress(emTo);
MailMessage msg = new MailMessage(from, to);

try
{

msg.Subject = stringemSubject;
msg.Body = emBody;
Attachment data = new Attachment(emAttach);
msg.Attachments.Add(data);

SmtpClient client = new SmtpClient(“smtp.gmail.com”);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(msg);
IsSent = true;

}
catch
{
msg.Dispose();
}
return IsSent;

}

public static bool EmailReportWithoutAttachment(string emTo, string emFrom,string emCC, string stringemSubject, string emBody)
{
bool IsSent = false;
string strToAddress = emTo;
string strFromAddress = emFrom;
string strCcAddress = emCC;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(strFromAddress, “Admin”);
mailMessage.To.Add(new MailAddress(strToAddress));
mailMessage.CC.Add(new MailAddress(strCcAddress));

mailMessage.Subject = stringemSubject; ;
mailMessage.IsBodyHtml = true;

mailMessage.Body = emBody;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
try
{
smtp.Send(mailMessage);
IsSent = true;
}
catch (Exception ex)
{
throw (ex);
}
mailMessage.Dispose();
return IsSent;
}
}
}

Advertisements

About Gokul Dahal, Nepal

Software Engineer, Freelancer, Design and Develop web applications,Software Developer Nepal, FreeLancer Nepal Contact : gokuldahal@gmail.com
This entry was posted in ASP.NET and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s