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:

Sitecore® Technology MVP 2019

Featured Post

Sitecore Error Troubleshooting: Sitecore Solr Connectivity Issue:: Part-1

Sitecore Error Troubleshooting: Sitecore Solr Connectivity Issue:: Part-1 How to verify Sitecore 9.1 (IaaS and Azure PaaS) + Solr (IaaS) C...

Total Pageviews

State Counder

Popular

About Me

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

Followers

Follow me on Twitter

Labels