Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
HOW can I Read and send mails in asp.net using c#? plz suggest me some code...
Posted 14-Dec-12 18:40pm
Comments
Mohamed Mitwalli at 15-Dec-12 0:49am
   
Google Before post your Question
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

T
Quote:
he Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet . SMTP protocol is using for sending email from C#. SMTP stands for Simple Mail Transfer Protocol . C# using System.Net.Mail namespace for sending email . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .
 
The following C# source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.
 
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.Credentials =
 
new System.Net.NetworkCredential("username", "password");

 
using System;
using System.Windows.Forms;
using System.Net.Mail;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
 
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";
 
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
 
Quote:
You have to provide the necessary information like your gmail username and password etc.

 
To Receive Email:
1: {www.codeproject.com/KB/IP/Pop3MailClient.aspx[^]}
2:{A POP3 Client in C# .NET[^]}
3:{http://dotnet.mvps.org/dotnet/faqs/?id=email&lang=en[^]}
  Permalink  
v4
Comments
live2anuj at 15-Dec-12 0:56am
   
This code only can send the mail from my account ,i want to receive mails also...
01010RAJ at 15-Dec-12 1:01am
   
k,wait
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Send Mail :
var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
 
Read Mail : (source for reading mail is from here)
 
ASPX :-
<form id="form1" runat="server">
<div>
From: <asp:Label ID="lblFrom" runat="server" Text="" />
<br />
Subject: <asp:Label ID="lblSubject" runat="server" Text="" />
<br />
Body: <asp:Label ID="lblBody" runat="server" Text="" />
</div>
</form>
 
ASPX.cs :-
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Pop3Client pop3Client = (Pop3Client)Session["Pop3Client"];
        int messageNumber = int.Parse(Request.QueryString["MessageNumber"]);
        Message message = pop3Client.GetMessage(messageNumber);
        MessagePart messagePart = message.MessagePart.MessageParts[0];
        lblFrom.Text = message.Headers.From.Address;
        lblSubject.Text = message.Headers.Subject;
        lblBody.Text = messagePart.BodyEncoding.GetString(messagePart.Body);
    }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100