ASP.NET - send and recieve mails - Asked By goldy gupta on 04-Feb-10 12:52 AM

I am making a new ASP.NET web application..

Here I wants to give user the facillity to send mails and recieve mails in my application..

I want this for gmail and hot mail..

what coding will be required for this..


Thanks in advance....

Venkat K replied to goldy gupta on 04-Feb-10 01:08 AM

Here is a nice small sample application to send an email:

If you want to restrict to only gmail and hotmail users then you can use a regular expression to validate the email address given in text box.

Validation the Gmail / Hotmail address:

1. We can use the following validation expression to ensure the email address is valid.

2. Then you can use another expression to validate whether the email address is free.


If validation has pass, it proved to include 'hotmail','gmail' free email.


Sakshi a replied to goldy gupta on 04-Feb-10 01:30 AM

Complete set of coding that can be used directly below,

DL M replied to goldy gupta on 04-Feb-10 01:57 AM
Use this code sample to send and receive using Gmail account .

protected void Button1_Click(object sender, EventArgs e)
MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.Subject = "Email using Gmail";

string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ""; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;

Huggy Bear replied to goldy gupta on 04-Feb-10 02:07 AM
In order to send emails you should have a valid SMTP server for relaying the emails, that your application sends.
Also you should create a valid account for receiving emails.

Include the namespace System.Net.Mail in order to make use of the below function. Note that my comments are inlined

public void SendEmail(string smtpHost, string subject, string body, string fromId, string toId)
    string replyToId = "";
    MailMessage message = new MailMessage();
    message.Subject = subject;
    message.Body = body;
    //Set this in order to represent the email body as a HTML text and format accordingly
    message.IsBodyHtml = true;
    message.From = new MailAddress(fromId);
    message.To.Add(new MailAddress(toId));
    //This will the id which will be in the To list, when user clicks on reply in his mail box.
    //so properly configure it
    message.ReplyTo = new MailAddress(replyToId);

    SmtpClient client = new SmtpClient(smtpHost);
mv ark replied to goldy gupta on 04-Feb-10 11:31 PM
Simple Mail Transfer Protocol (SMTP) server handles the sending of your email messages to the Internet while a Post Office Protocol 3 (POP3) server holds incoming email messages until you check your email.

This code snippet shows how you can send an email programmatically using C# with GMail -

Adapt the same code using the Hotmail credentials mentioned below to send an email programmatically with Hotmail

You can retrieve Mails from Gmail & Hotmail with POP3 component like Indy.Sockets:

Before you fetch mails from Gmail, you will have to enable POP access explicitly -

These are the details to use:
Gmail -
Use SSL: Yes
Port: 995

Hotmail -
Use SSL: Yes
Port: 995

SMTP server: (Port 25) {Note: If port 25 has been blocked in your network or by your ISP, you can set SMTP port to 587 with TLS or SSL Encryption depending on the client in use}

Gmail provides free POP access -

Free POP3 feature in Windows Live Hotmail
paul z replied to DL M on 04-Jul-11 03:39 PM
But I still cannot find out how to receive.