Click here to Skip to main content
15,740,620 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)


    protected void btnSend_Click(object sender, EventArgs e)
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;
        if (FileUpload1.HasFile)
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        SmtpClient smtp = new SmtpClient();
        smtp.Host = ""; 
        smtp.Credentials = new System.Net.NetworkCredential("", "YourGmailPassword");
        smtp.EnableSsl = true;

above is the code i used but i am unable to receive the mail .... what can be the possible problem please help ..
Updated 20-Jun-12 19:38pm

in web.config file add these lines i think it will solve your problem

      <smtp deliveryMethod="PickupDirectoryFromIis"/>
    <authentication mode="Forms" />
Share this answer
singlaNitish 21-Jun-12 1:21am    
still unable to receive any mail
singlaNitish 21-Jun-12 1:24am    
for your information i have not yet deployed the website . i am using development server to check
I believe that you need to send email via GMail, please set the property of Port on the SmtpClient object (in your case it is smtp)to 465 or 587.
Share this answer
singlaNitish 21-Jun-12 1:35am    
still not working
Muthu Vinoth Kumar 21-Jun-12 2:22am    
i try ur code.. it works fine
Pankaj Nikam 21-Jun-12 4:17am    
Thanks. Please mark it as answer if it solves your problem.
bbirajdar 21-Jun-12 3:04am    
If it works, then please mark it as answer
add one more Line your code
smtp.UseDefaultCredentials = false;

i hope it work.
Share this answer
using System.Net.Mail;
using System.Net.Mime;
using System.IO;

public partial class _Default : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)

        //---------Record Sending by Email------------------------------------
        MailMessage mailObj = new MailMessage(MailFrom.Text, MailTo.Text, Head.Text, Message.Text);

        SmtpClient SMPTServer = new SmtpClient(""); 
        //Various Email servers do not support "SMTP", write "" instead of that.
        //SMPTServer.EnableSsl = true; //Enable this if doesn't work...

        SMPTServer.Credentials = new System.Net.NetworkCredential("", "password");


        catch (Exception ex)
Share this answer
singlaNitish 21-Jun-12 12:03pm    
though the code given by you is working but i am unable to figure out mistake in my code can you help me please.
try the following code
SmtpClient serverobj = new SmtpClient();
string username = "your gmail is";
string pwd1 = "gmail password";
serverobj.Credentials = new NetworkCredential(username, pwd1);
serverobj.Port = 25;
serverobj.Host = "";
serverobj.EnableSsl = true;
MailMessage msobj = new MailMessage();
msobj.From = new MailAddress(user, "Email", System.Text.Encoding.UTF8);
string str = "";
msobj.Subject = "New Event Inforamtion";
msobj.Body = desc.Text;

Share this answer

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