Click here to Skip to main content
15,889,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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.To.Add(txtTo.Text);
        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.gmail.com"; 
        smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

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

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

XML
<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="PickupDirectoryFromIis"/>
    </mailSettings>
  </system.net>
  <system.web>
    <authentication mode="Forms" />
  </system.web>
 
Share this answer
 
Comments
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 asp.net 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
 
Comments
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
Please
add one more Line your code
C#
smtp.UseDefaultCredentials = false;

i hope it work.
 
Share this answer
 
v2
C#
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("smtp.gmail.com"); 
        //Various Email servers do not support "SMTP", write "mail.server.com" instead of that.
        
        //SMPTServer.EnableSsl = true; //Enable this if doesn't work...

        SMPTServer.Credentials = new System.Net.NetworkCredential("your-email@address.com", "password");

        try
        {
            SMPTServer.Send(mailObj);            
            Response.Redirect(@"~\Default.aspx");            
        }

        catch (Exception ex)
        {
            Response.Write(ex);
        }        
    }
}
 
Share this answer
 
v4
Comments
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 = "smtp.gmail.com";
serverobj.EnableSsl = true;
MailMessage msobj = new MailMessage();
msobj.From = new MailAddress(user, "Email", System.Text.Encoding.UTF8);
string str = "samplename@gmail.com";
msobj.To.Add(str);
msobj.Subject = "New Event Inforamtion";
msobj.Body = desc.Text;

serverobj.Send(msobj);
 
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