Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Gmail credentials to send mail in .Net application

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 409
    Comment on it

    Lets consider a task on hand , we need to add a contact us form which takes in few inputs from the user and on submission mails the details to the administrator. The requirement says that the mail should be sent using Gmail SMTP client. The solution is not difficult to integrate

    try
       {
            const string fromAddress = "from@gmail.com";  // gmail address from which you want to send the email
            const string toAddress = "to@xyz.com";  //  email address of the recipient
            const string fromPassword = "password";  // password of the gmail account being used
            const string subject = "Subject";  // subject line of the email
            const string body = "Body";  // email content
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); // smtp.gmail.com is the host and 587 is the port used to send emails
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            MailMessage message = new MailMessage(fromAddress, toAddress,subject,body);
            smtp.Send(message);  // sends the email
         }catch(SmtpException ex)
         {
                    //Exception handling
          }
          catch(Exception ex)
         {
                    //Exception handling
         }
    

    Dont forget to include

    using System.Net;
    using System.Net.Mail;
    

    System.Web is now deprecated so, please use System.net

    There are few catch points in integrating this code packet. Many a times you will get an authentication failed error this might come due to various reasons

    • Weak password : The error does not mention anything related to passwords but its one of the main reasons. Solution is to change to a stronger password.
    • The code gives error on deployment to another server : Due to strict authentication checks, Google sometimes blocks sign-in from applications if accessed from unknown locations. This can be the case especially if you have been developing and testing the application on one location and then deployed on another server. Solution is to connect to the server through RDP if possible and sign-in to the Gmail id used in a browser.
    • None of the above : The code build does not throw any error and you have written the code after lot of verifications, but the following exception is thrown at the run-time The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. This comes quite often when you have enabled two step verification on Gmail ID you are using or the less secure apps are not allowed. The resolution is to switch off the two step verification and Allow less secure apps from https://myaccount.google.com/security

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: