Friday, May 16, 2008

Send Mail using System.Web.Mail object in C#(C-Sharp):

Send Mail using System.Web.Mail object in C#(C-Sharp):

In Visual Studio 2005 microsoft provided the new mail component called System.Web.Mail for sending email. In this article I am defining you that how can you send the mail in HTML format with attachment.

Add the reference to System.Web.Mail. In this article I am using the function

sendMail for sending email with attachment. The definition of function is

sendMail(string strFrom, string MailID, string strLogFile). In this strFrom variable contains the email address of person who are sending the mail, MailID contains the email addresses of person to whom mail would be send and strLogFile contain the physical path of log file, which send as a attachment. The body part of the function is:

public void sendMail(string strFrom, string MailID, string strLogFile)

{
string strMessage;
string ToMail_ID;
string strYourEmail = strFrom; // from mail id
System.Web.Mail.MailMessage sMail = new MailMessage();
ToMail_ID = MailID; // " LIST OF TO MAIL ID"
sMail.To = ToMail_ID;

sMail.From = strYourEmail;

sMail.BodyFormat = MailFormat.Html;

sMail.Priority = MailPriority.High;

sMail.Attachments.Add(new System.Web.Mail.MailAttachment(strLogFile));

strMessage = "It is test mail.";
sMail.Body = strMessage;

sMail.Subject = “Test Mail”;

SmtpMail.SmtpServer="";

try

{

SmtpMail.Send(sMail);

}

catch (Exception ex)

{

//check the InnerException

while (ex.InnerException != null)

{

ex = ex.InnerException;

}

}

}


Now you can call the function as:
sendMail("mcapassion@gmail.com","mcapassion@gmail.com,a@a.com", "C:\TestMail.log");


Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels