Here is an example of sending an email in ASP.NET using the SMTP server.

First, add the namespace.

using System.Web.Mail;

And the function.

C#

private bool SendMail(String SendTo, String sendToCC, string SendFrom, string Subject, string msg)
{
    bool bSuccess;
    try
    {
  
        MailMessage mail = new MailMessage();
        mail.To = SendTo;
        mail.Cc = sendToCC; // to send a carbon copy
        mail.From = SendFrom;
        mail.Subject = Subject;
        mail.Body = msg;
        SmtpMail.SmtpServer = "You smtp server";

        SmtpMail.Send(mail);

        bSuccess = true;
    }
    catch 
    {
        bSuccess = false;
    }

    return bSuccess;
}

VB.NET

Private Function SendMail(SendTo As [String], sendToCC As [String], SendFrom As String, Subject As String, msg As String) As Boolean
  Dim bSuccess As Boolean
  Try

    Dim mail As New MailMessage()
    mail.[To] = SendTo
    mail.Cc = sendToCC
    ' to send a carbon copy
    mail.From = SendFrom
    mail.Subject = Subject
    mail.Body = msg
    SmtpMail.SmtpServer = "You smtp server"

    SmtpMail.Send(mail)

    bSuccess = True
  Catch
    bSuccess = False
  End Try

  Return bSuccess
End Function

 

Last modified: March 6, 2019

Comments

Write a Reply or Comment

Your email address will not be published.