Friday, August 16, 2013

Sending mail using gmail.smtp in ASP.Net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;


using Microsoft.Win32;




private void btnSendMail_Click(object sender, EventArgs e)
        {
            try
            {
                objBUserInfo.UserName = txtUserName.Text;
                DataTable dt = objBUserInfo.GetUserInfo();

                SmtpClient client = new SmtpClient();

                MailAddress from = new MailAddress("thegautamgupta1990@gmail.com",
                   "Gautam" + " Gupta",
                System.Text.Encoding.UTF8);

                MailAddress to = new MailAddress(dt.Rows[0]["email"].ToString());

                MailMessage message = new MailMessage(from, to);
                message.Body = "This is a test e-mail message sent by an application. ";

                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "Your Password is " + dt.Rows[0]["Password"] + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;

                client.Host = "smtp.gmail.com";
                client.Port = 587;

                client.EnableSsl = true;
                NetworkCredential nc = new NetworkCredential("gmailaddresshere","PasswordHere");
                client.Credentials = nc;
                client.SendCompleted += new
                SendCompletedEventHandler(a);
                client.Send(message);
            }
            catch
            {

            }

        }