Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NETmailing
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.
 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 17 Dec '12 - 3:17
Edited 17 Dec '12 - 4:20

Comments
ridoy - 17 Dec '12 - 15:00
where is your port number to send a mail?..i think you miss that

2 solutions

What's the error you are getting? Have a look at this sample:
Send Mail / Contact Form using ASP.NET and C#[^]
  Permalink  
Comments
Kavitha le - 17 Dec '12 - 9:21
Could not send the e-mail - error: Failure sending mail.
zafar sultan - 17 Dec '12 - 9:24
I have updated the reply. Check the sample and see if it works for you.
Kavitha le - 17 Dec '12 - 9:36
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?
Armando Talex - 17 Dec '12 - 10:56
Make sure that you are having the correct value for the following values. smtp.Host = "your server host address"; smtp.Port = server port number;
Kavitha le - 18 Dec '12 - 9:24
How will i get server port number?
zafar sultan - 18 Dec '12 - 10:22
Can you share your code? because all the solutions suggested to you does work. regarding port number, it is the port number that your SMTP server use for e-mails. In general, it is 25. For example, Gmail's SMTP server use port 587 as described in the solution below.
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);
  Permalink  
Comments
Kavitha le - 18 Dec '12 - 5:00
Hai Krunal, Can you please give the complete code? here where should i add this?
Kavitha le - 18 Dec '12 - 5:47
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 419
1 Mahesh Bailwal 343
2 CPallini 245
3 Maciej Los 230
4 Aarti Meswania 173
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,194
2 CPallini 3,923
3 Rohan Leuva 3,176
4 Maciej Los 2,633


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid