Home > ASP.Net > Send Email using Gmail Account

Send Email using Gmail Account


Hi

try this example to Send Email using Gmail Account

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 Send_Email : System.Web.UI.Page
{
    private bool SendMail(string from, string body, string subject)
    {
        try
        {
            string mailServerName = "smtp.gmail.com";
            MailMessage message = new MailMessage(from, "<a href="mailto:username@gmail.com">username@gmail.com</a>", subject, body);
            message.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = mailServerName;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "<a href="mailto:username@gmail.com">username@gmail.com</a>";
            NetworkCred.Password = "xxxx";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('An Error Accured try

again.');</script>");
        }
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        txt_Email.Attributes.Add("onpaste", "return false;");
        txt_Message.Attributes.Add("onpaste", "return false;");
    }

    protected void btn_Send_Click(object sender, EventArgs e)
    {
        bool x = SendMail(txt_Email.Text, txt_Message.Text, txt_Subject.Text);
        if (x == true)
        {
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Message

Sent');</script>");
            Response.Redirect(Request.Url.ToString(), false);
        }
        else
        {
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Try again');</script>");
        }
    }
}

and in web.config file add this :-

<system.net>
    <mailSettings>
      <smtp from="<a href="mailto:username@gmail.com">username@gmail.com</a>">
        <network host="smtp.gmail.com" port="587" userName="<a href="mailto:username@gmail.com">username@gmail.com</a>" password="xxxx" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
  </system.net>

Hope this helps

Advertisements
Categories: ASP.Net
  1. Mazen
    November 2, 2009 at 2:06 pm

    السلام عليكم

    مشكووووور أسأل الله لك التوفيق ..

    ظهرت لي 3 أخطاء ما أدري وش الأسباب..

    – the name Text_Email does not exist in the current context
    – the name Text_Message does not exist in the current context
    – expexted catch or finally ( at default.aspx.cs line: 66)

    ممكن توضح لي كيف أتخلص منها ..؟؟؟

  2. yasserzaid
    November 2, 2009 at 7:51 pm

    Dear Mazen thanks for your comment , hope my post helps you
    about the Error you got
    – Error 1 :-
    the name Text_Email does not exist in the current context
    You should add TextBox with ID =”txt_Email” which will contain the email of user which will send Email to you or to anyone.

    – Error 2 :-
    the name Text_Message does not exist in the current context
    This mean you should add TextBox control with ID= “txt_Message” which will contain message or body of Email

    – Error 3 :-
    expexted catch or finally ( at default.aspx.cs line: 66)
    This mean you should use Catch to handle any Exception that can happen

    Hope this helps
    if you have any question send me
    Good Luck

  3. ehab lotfy
    December 23, 2010 at 2:22 pm

    thanxxxxxxxxxxxx ya yaser

  4. yasserzaid
    December 26, 2010 at 11:29 am

    You are Welcome Ehab 🙂

  5. February 22, 2014 at 11:05 am

    I am sure this post has touched all the internet visitors, its really really fastidious piece
    of writing on building up new web site.

  6. Johna651
    June 5, 2014 at 8:27 pm

    I like this post, enjoyed this one thank you for putting up. No man is wise enough by himself. by Titus Maccius Plautus. ggffkdedkcge

  1. July 2, 2014 at 11:40 pm
  2. July 2, 2014 at 11:41 pm
  3. July 4, 2014 at 9:28 pm
  4. July 4, 2014 at 9:28 pm
  5. July 6, 2014 at 6:24 am

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: