0

I'm trying to send email to my site users (ASP.NET, VS2010), currently I'm using my gmail account for sending email, but I'm getting the following error:

ex = {"Failure sending mail."}
InnerException = {"The remote name could not be resolved: 'smtp@gmail.com'"}

it is my code:

MailMessage mailObj = new MailMessage("mygmailaccount@gmail.com", "myyahooaccount@yahoo.com", "test", "test2");
            SmtpClient SMTPServer = new SmtpClient("smtp@gmail.com");
            SMTPServer.Credentials = new System.Net.NetworkCredential("mygmailaccount", mygmailpassword);
            try
            {
                SMTPServer.Send(mailObj);
            }
            catch (Exception ex)
            {
                string a = ex.Message;
            }

what is going wrong here? should I do something in my web.config? how can I find smtp server of my own host?

Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115

2 Answers2

6

smtp@gmail.com is incorrect. You probably meant smtp.gmail.com. See the following question for a complete example.

Sending email in .NET through Gmail

Community
  • 1
  • 1
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • thanks, I changed it to smtp.gmail.com and got following error:Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. x22sm13327733bkw.11" – Ali_dotNet Feb 16 '12 at 14:35
  • Look at the answer in the link. The solution is right there. You need to enable SSL and run on port 587 – Morten Anderson Feb 16 '12 at 14:36
  • yes, thanks, that was just great! but what if I'm going to send email from my own host? how can I find its port and smtp? – Ali_dotNet Feb 16 '12 at 14:39
  • You will have to ask your email administrator or hosting provider. – jrummell Feb 16 '12 at 14:43
1

you did not set gmail parameters correctly:

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
    smtpClient.EnableSsl = true;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.Credentials = new NetworkCredential("myGmailAcconut@gmail.com", "password");
enricoariel
  • 483
  • 2
  • 10