Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HOW can I Read and send mails in asp.net using c#? plz suggest me some code...
Posted
Comments
Mohamed Mitwalli 15-Dec-12 0:49am    
Google Before post your Question

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[^]}
 
Share this answer
 
v4
Comments
live2anuj 15-Dec-12 0:56am    
This code only can send the mail from my account ,i want to receive mails also...
StackQ 15-Dec-12 1:01am    
k,wait
Send Mail :
C#
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 :-
XML
<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);
    }
}
 
Share this answer
 
Comments
Member 9579525 5-Apr-18 2:55am    
Which dll's reference need to add for pop3Client?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900