How To Send Email Using Gmail SMTP In ASP.NET (C#)

Email sending feature in a web application is a common thing now-a-days. We came across this feature numerous times may it be your contact us form, or subscribe form or enquiry form. 
Do you really know how the ASP.NET application send email ? Here is how the ASP.NET application sends email to any valid email address using the Gmail SMTP credentials. 
In this case the SMTP client used is Gmail, you can use your own SMTP client to send email too.



Code to Send Email using Gmail SMTP in C#


First your application need a reference to "System.Net" namespace. Another namespace named "System.Net.Mail" will be needed in the application, so add it up. Here is the code block to send the email.

public bool SendEmailViaGmail(int requestId, string assignedByUserName)
        {
            try
            {
                //Assign the smtp credentials for gmail
                SmtpClient smtp = new SmtpClient();
                if (true)
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("from emil address", "password");
                    smtp.Timeout = 20000;
                }

                MailAddress fromAddress = new MailAddress("[email protected]", "From Name");
                MailAddress toAddress = new MailAddress("[email protected]");
                MailAddress ccAddress = new MailAddress("[email protected]");

                //Passing values to smtp object
                dynamic message = new MailMessage(fromAddress, toAddress);
                message.CC.Add(ccAddress);
                message.Subject = "Mail Subject";
                message.Body = "Mail Body";
                message.IsBodyHtml = true;

                //Send email
                smtp.Send(message);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

The above code is very simple to get what I am doing here. This piece of code is responsible to send email using the Gmail id you have given from your local machine though you have not hosted the application in any server.

If you want to use your own SMTP server then just change few things change the smtp.Host name to your host name, smtp.Port name to your port name etc.

That's it ! You are done with the work.



Happy Coding...

3 comments:

  1. 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. s21sm24848685pfi.29 - gsmtp
    _____________________________________________________________________________________

    This problem facing at the time of sending mail

    ReplyDelete
  2. Hi,

    this code doesn't work - using this (but updating user and password to our own gives:

    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)

    I have reason to believe it now isn't possible to use GMail externally to Google wihtout going through the API because Google are so useless, that even with the correct username and password, they still block the request, leading to more unnecessary work at no extra benefit.

    Fucking retards.

    ReplyDelete
  3. And, if you are a Gmail user, you do not need to worry about anything. Besides the official Gmail technical support services, the third party Gmail customer service providers are also available at your closest disposal, and you can get any kind of technical support as and when required.
    Gmail Support

    ReplyDelete