Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET mailing
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 4:17am
Kavitha_b1.4K
Edited 17-Dec-12 5:20am
v3
Comments
ridoy at 17-Dec-12 15:00pm
   
where is your port number to send a mail?..i think you miss that
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What's the error you are getting? Have a look at this sample:
Send Mail / Contact Form using ASP.NET and C#[^]
  Permalink  
v2
Comments
Kavitha le at 17-Dec-12 9:21am
   
Could not send the e-mail - error: Failure sending mail.
zafar sultan at 17-Dec-12 9:24am
   
I have updated the reply. Check the sample and see if it works for you.
Kavitha le at 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?
Kavitha le at 18-Dec-12 9:24am
   
How will i get server port number?
zafar sultan at 18-Dec-12 10:22am
   
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 18-Dec-12 5:00am
   
Hai Krunal,
Can you please give the complete code?
here where should i add this?
Kavitha le at 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)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 304
2 Richard MacCutchan 225
3 BillWoodruff 169
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100