ASP.net,C# Code For Sending An Email using Gmail and Hotmail


ASP.net,C# Code Send Mail From GMail Account

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
  protected void btn_Click(object sender, EventArgs e)
    {
       MailMessage MyMailMessage = new MailMessage("FromMailId@gmail.com", "TOMailId@hotmail.com",
"write your subject Here ", "Hi,This is the test message ");
MyMailMessage.IsBodyHtml = false;
NetworkCredential mailAuthentication = new NetworkCredential("FromMailId@gmail.com", "Password");
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;
        mailClient.Send(MyMailMessage);
     }
}

ASP.net,C# Code Send Mail From Hotmail Account

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
  protected void btn_Click(object sender, EventArgs e)
    {
       MailMessage MyMailMessage = new MailMessage("FromMailId@hotmail.com", "TOMailId@yahoo.com",
"write your subject Here ", "Hi,This is the test message. ");
MyMailMessage.IsBodyHtml = false;
NetworkCredential mailAuthentication = new NetworkCredential("FromMailId@hotmail.com", "Password");
        SmtpClient mailClient = new SmtpClient("smtp.live.com", 25);
        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;
        mailClient.Send(MyMailMessage);
     }
}
Advertisements

4 thoughts on “ASP.net,C# Code For Sending An Email using Gmail and Hotmail

  1. I tried to run your code in Visual Studio 2010 with my credentials (which I’ve removed below) as follows but it threw exception;
    ————————————————————-

    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.Web;
    using System.Net;
    using System.Net.Mail;

    namespace mail_mail
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

    MailMessage MyMailMessage = new MailMessage(“FromMailId@gmail.com”, “FromMailId@gmail.com”,

    “write your subject Here “, “Hi,This is the test message “);

    MyMailMessage.IsBodyHtml = false;

    NetworkCredential mailAuthentication = new NetworkCredential(“FromMailId@gmail.com”, “password”);

    SmtpClient mailClient = new SmtpClient(“smtp.gmail.com”, 587);

    mailClient.EnableSsl = true;

    mailClient.UseDefaultCredentials = false;

    mailClient.Credentials = mailAuthentication;

    mailClient.Send(MyMailMessage);

    }

    }
    }

    ————————————————————-
    This is the exception:@line –> mailClient.Send(MyMailMessage);

    System.Net.Mail.SmtpException was unhandled
    Message=Failure sending mail.

    Please Help me get out of this exception!

  2. Very good post. I learn something tougher on different blogs everyday. It’ll at all times be stimulating to learn content from other writers and observe somewhat one thing from their store. I’d select to use some with the content on my blog whether or not you don’t mind. Natually I’ll offer you a link in your internet weblog. Thanks for sharing.

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