Send email with attachment - .net

I have seen many posts over forums asking for how to send emails in asp.net.

In this blog post, I am going to post code snippet to send email in asp.net (with attachment).

Check out Below Code to send email with attachment in asp.net:

public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
{
try
{
// Create the mail message
MailMessage objMailMsg = new MailMessage(strFrom, strTo);

objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = strSubject;
objMailMsg.Body = strMsg;
Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
objMailMsg.Attachments.Add(at);
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;

//prepare to send mail via SMTP transport
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
objSMTPClient.Send(objMailMsg);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

Contact Form

Name

Email *

Message *

Popular Posts

Life Cycle of a Web Request

Python Keywords

Internal Revenue Service (IRS) - Questions About Your Tax Refunds In US

Role Of Thyroid Gland In Our Body And Its Effects On Weight Loss And Gain

Why do we blow out candles on our birthdays? - A deep insight into it

How to create the FAANG: Resume Template for Software Engineer?