Thursday, 26 July 2012

How to send mail through asp.net using c# or VB.NET | Sending Mail in ASP.NET using C# or VB.NET

                                  Hi friends, in this article I will explain about how to send email using Gmail in VB.NET or C#.
                                 Sending email's from web applications is a very common requirement in most projects. This article explores how to send Emails in ASP.NET.In ASP.NET, sending emails has become simpler. The classes required to send an email are contained in the System.Net.Mail.
  • First   Imports or using the System.Net.Mail namespace.
  • Create a MailMessage object. This MailMessage class contains the actual message what you want to send to your friends.
  • Dim mail As MailMessage = New MailMessage()
  • The constructor of this MailMessage class is used to specify the sender email, receiver email, subject, body.
  • After creating a message, use the SmtpClient to send the email to the specified SMTP server. I am using Gmail SMTP server smtp.gmail.com.


Write or copy and paste the below code.

Imports System.Net.Mail
Partial Class sendmail
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mail As MailMessage = New MailMessage()
        mail.To.Add("Your Email address like xxxxxxx@gmail.com")

        mail.From = New MailAddress("xxxxx@gmail.com")
        mail.Subject = "Aspdotnet-kishore: Send Email using VB.NET"

        Dim Body = "Hi, This is the test mail to send mail using Gmail in ASP.NET"
        mail.Body = Body

        mail.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.gmail.com" 'Or  SMTP Server Address
        smtp.Credentials = New System.Net.NetworkCredential("Your Email address like xxxxxxx@gmail.com", "password")
        smtp.EnableSsl = True
        smtp.Send(mail)

    End Sub
End Class


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;
partial class sendmail : System.Web.UI.Page
{
     protected void Button1_Click(object sender, System.EventArgs e)
     {
          MailMessage mail = new MailMessage();
          mail.To.Add("Your Email address like xxxxxxx@gmail.com");

          mail.From = new MailAddress("xxxxx@gmail.com");
          mail.Subject = "Aspdotnet-kishore: Send Email using VB.NET";

          dynamic Body = "Hi, This is the test mail to send mail using Gmail in ASP.NET";
          mail.Body = Body;

          mail.IsBodyHtml = true;
          SmtpClient smtp = new SmtpClient();
          smtp.Host = "smtp.gmail.com";
          //Or  SMTP Server Address
          smtp.Credentials = new System.Net.NetworkCredential("Your Email address like xxxxxxx@gmail.com""password");
          smtp.EnableSsl = true;
          smtp.Send(mail);

     }
}

                                   In the above code replace the  xxxxxxx@gmail.com  with the email address of what address you want to send and Your Email address like xxxxxxx@gmail.com", "password with the your email address password.When you execute the above code the email is automatically send to your email address .

Read How to send emails using HTML Templates in ASP.NET using C#/VB.NET

Go to another interview question ASP.NET Validation Controls with example | Range validator in ASP.NET and how to use the range validator in ASP.NET

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.