Saturday, August 22, 2009

Send mail from Asp.Net

Use follwing class for sending Mail ,attachment in asp.net

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

///
/// Email class
///

public static class Email
{
public static bool SendMail(string msgFrom, string msgTo, string msgSubject, string msgBody, bool highPriority,string Filename)
{
MailMessage mailMsg = new MailMessage();
SmtpClient smtp = new SmtpClient();
mailMsg.From = new MailAddress(msgFrom);
mailMsg.To.Add(msgTo);
mailMsg.Subject = msgSubject;
mailMsg.Body = msgBody;
mailMsg.IsBodyHtml = true;
if (highPriority) mailMsg.Priority = MailPriority.High;

if (Filename != "")
{
Attachment attachfile = new Attachment(Filename);
mailMsg.Attachments.Add(attachfile);
}
try
{
smtp.Send(mailMsg);
return true;
}
catch (Exception myEx)
{
return false;
}
}
}

No comments: