Send email from asp.net

Send email from asp.net: 

If one want to send email from website using code, one can send using bellow sample code, this is only possible with SMTP(Simple Mail Transfer Protocol).  We need to add name space System.Web.Mail as shown in sample code

public bool SendEMail()
{
    try
    {
        System.Web.Mail.MailMessage msg = new MailMessage();
        msg.BodyFormat = System.Web.Mail.MailFormat.Html;
        msg.To = To;
        msg.Cc = Cc;
        msg.From = From;
        msg.Subject = Subject;
        msg.Body = Body;
        int j;
        if (m_attachments.Count > 0)
        {
            foreach (string strAttachment in m_attachments)
            {
                System.Web.Mail.MailAttachment attachment = new MailAttachment(strAttachment);
                msg.Attachments.Add(attachment);
            }
        }
        msg.BodyFormat = MailFormat.Html;
        // GET THE SMTP SERVERNAME FROM THE CONFIG FILE //
        SmtpMail.SmtpServer = ConfigurationManager.AppSettings["SMTPServerName"];

        SmtpMail.Send(msg);
    }
    catch (Exception ex)
    {
        msgSendFlag = false;
        throw ex;
    }
    return msgSendFlag;
}

 

Leave a Reply