Home > ASP.Net > Send Asynchronous Email using Gmail Account

Send Asynchronous Email using Gmail Account


Hi

try this example to Send Asynchronous email

1) Create a new web site

2) In Web.Config file add this :-

<system.net>
  <mailSettings>
   <smtp from="youremail@gmail.com">
    <network host="smtp.gmail.com" defaultCredentials="false" port="587" userName="youremail@gmail.com" password="xxxxx"/>
   </smtp>
  </mailSettings>
 </system.net>

3) Add new web page and in code behind :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
            mailMessage.To.Add(new MailAddress("any email"));
            mailMessage.To.Add(new MailAddress("any email"));
            mailMessage.From = new MailAddress("any email");
            mailMessage.Subject = "my subject";
            mailMessage.Body = "my body";
            mailMessage.IsBodyHtml = true;
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.EnableSsl = true;
            object userState = mailMessage;
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
            smtpClient.SendAsync(mailMessage, userState);
        }
        catch
        {

        }
    }

    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        try
        {
            MailMessage mailMessage = default(MailMessage);
            mailMessage = (MailMessage)e.UserState;
            if ((e.Cancelled))
            {
                lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To[0].Address;
            }
            if ((e.Error != null))
            {
                lblMessage.Text = "Error occured, info :" + e.Error.Message;
            }
            else
            {
                lblMessage.Text = "Mail sent successfully";
            }
        }
        catch (Exception ex)
        {

        }
    }   
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: