Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that sends me a mail using this code:
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "mymailuser";
WebMail.Password = "mypassword";
WebMail.From = "mymailuser@gmail.com";

WebMail.Send("party-host@example.com", "RSVP Notification",
    Model.Name + " is " + ((Model.WillAttend ?? false) ? "" : "not")
        + "attending");


The form reports a successful send but I don't receive any mail (naturally I am using a valid username and password).

Bizzarely all I get in my inbox is a delivery note failure attempting to contact to my long defunct 02 mail account. I haven't referenced it anywhere in my code!!
Posted

What about this,
public void SendEmail(string from, string to, string subject, string body)
{
    string host = "smtp.gmail.com";
    int port = 587;

    MailMessage msg = new MailMessage(from, to, subject, body);
    SmtpClient smtp = new SmtpClient(host, port);

    string username = "example@gmail.com";
    string password = "password";

    smtp.Credentials = new NetworkCredential(username, password);
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;

    try
    {    
        smtp.Send(msg);
    }
    catch (Exception ex)
    {
        // do something with it...
    }
}


-KR
 
Share this answer
 
v2
Turns out I had an auto format on the Gmail account, and it is only obvious when I send mails from me to me. A quick test in outlook exhibited the same behaviour. Once removed all behaved as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900