Wednesday 5 June 2013

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

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");

No comments:

Post a Comment