Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application I would like to send a mail and I am using the below code, but I get the error:
Could not send the e-mail - error: Failure sending mail.

C#
protected void btnsend_Click(object sender, EventArgs e)
   {
       try
       {
           MailMessage mailMessage = new MailMessage();
           mailMessage.To.Add("xxx@gmail.com");
           mailMessage.From = new MailAddress("xxxx@gmail.com");
           mailMessage.Subject = "ASP.NET e-mail test";
           mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
           SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
           smtpClient.Send(mailMessage);
           Response.Write("E-mail sent!");
       }
       catch (Exception ex)
       {
           Response.Write("Could not send the e-mail - error: " + ex.Message);
       }
   }

Here we are not giving the sender(from)password then how the system will send mail from that sender?
Posted
Updated 17-Dec-12 4:20am
v3
Comments
ridoy 17-Dec-12 15:00pm    
where is your port number to send a mail?..i think you miss that

What's the error you are getting? Have a look at this sample:
Send Mail / Contact Form using ASP.NET and C#[^]
 
Share this answer
 
v2
Comments
Am Gayathri 17-Dec-12 9:21am    
Could not send the e-mail - error: Failure sending mail.
Zafar Sultan 17-Dec-12 9:24am    
I have updated the reply. Check the sample and see if it works for you.
Am Gayathri 17-Dec-12 9:36am    
Ok i will check. But one doubt here we are not giving the sender(from)password then how the system will send mail from that sender?
[no name] 17-Dec-12 10:56am    
Make sure that you are having the correct value for the following values.
smtp.Host = "your server host address";
smtp.Port = server port number;
Am Gayathri 18-Dec-12 9:24am    
How will i get server port number?
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);
 
Share this answer
 
Comments
Am Gayathri 18-Dec-12 5:00am    
Hai Krunal,
Can you please give the complete code?
here where should i add this?
Am Gayathri 18-Dec-12 5:47am    
Error 1 The type or namespace name 'NetworkCredential' could not be found (are you missing a using directive or an assembly reference?)
getting this error while trying

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