[RESOLVED]using gmail smtp with WebMail

Hello,

In the _AppStart, I have :

    WebMail.SmtpServer = "smtp.gmail.com";
    WebMail.EnableSsl = true;
    WebMail.SmtpPort = 587;
    WebMail.UserName =  "ayanmesut";
    WebMail.Password = "mypassword";
    WebMail.From = "ayanmesut@gmail.com";

In the file:

           ...
           try
           {
            WebMail.Send(
                to: email,  
                subject: "There is a question!", 
                body: message,
                isBodyHtml: true
            ); 
           ...

I get this error:

System.Net.Mail.SmtpException: Failure sending mail. —> System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after
a period of time, or established connection failed because connected host has failed to respond 64.233.164.108:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) — End of inner exception stack trace — at System.Net.ServicePoint.GetConnection(PooledStream PooledStream,
Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject,
GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint
servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) — End of inner exception stack trace — at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Web.Helpers.WebMail.Send(String
to, String subject, String body, String from, String cc, IEnumerable`1 filesToAttach, Boolean isBodyHtml, IEnumerable`1 additionalHeaders, String bcc, String contentEncoding, String headerEncoding, String priority, String replyTo) at ASP._Page_ContactUs_cshtml.Execute()
in c:UsersMesDesktopWeb Development With WebMatrixEmptySiteAsp.netContactUs.cshtml:line 26

Google "send email using gmail asp.net" as this is probably ask at least once a day.  You’re probably missing the "UseDefaultCredentials = false" line, but look at some sample code and you’ll have an idea what you’re missing, and you’ll also find solutions
to other issues you’re likely to have.

In general though it is a bad idea to send automated email through gmail’s smtp servers.

Hello AidyF,

I added the following code:

    WebMail.SmtpUseDefaultCredentials = false; 

It does not help. I checked many solutions but I cannot get the result. I am using gmail because I just want to check that. In my real apps, I use hosting smtp service.

make the username field your complete gmail account email address.

I don’t have the isbodyhtml: true , but it still sends it as such

user name must be

WebMail.UserName =  "ayanmesut@gmail.com";

Hello,

I still have error:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean
allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Web.Helpers.WebMail.Send(String to, String subject, String body, String from, String cc, IEnumerable`1 filesToAttach, Boolean isBodyHtml,
IEnumerable`1 additionalHeaders, String bcc, String contentEncoding, String headerEncoding, String priority, String replyTo) at ASP._Page_ContactUs_cshtml.Execute() in c:UsersMesDesktopWeb Development With WebMatrixEmptySiteAsp.netContactUs.cshtml:line
26

1) either your password is wrong

2) check your gmail settings. Have you enabled the send email from applications options? Go to settings > forwarding and pop/imap and enable imap

thanx for your help.

I also did the following:

go to security.google.com/settings/security/activity and here you’ll see blocked connection from your code. Allow them.

Leave a Reply