TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

How To Send Email Using Gmail In C#

How To Send Email Using Gmail In C#

After Google introduced a Two-step verification system in Google Accounts, it's not easy to use Gmail for your own personal usage so to solve this problem, I figured out a way to use Gmail as an email medium to send emails using C#. Now, we will see how we can do this in some short steps.

Step 1: Write C# code for Email Service

Open your Visual Studio, Below is the C# code for the email service, now you have to replace "Sender Email", "Reciever Email", "Email Subject", and "Email Body". Lastly, we have to retrieve the Google app password.

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

namespace EmailApp
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            String SendMailFrom = "Sender Email";
            String SendMailTo = "Reciever Email";
            String SendMailSubject = "Email Subject";
            String SendMailBody = "Email Body";

            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587);
                SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage email = new MailMessage();
                // START
                email.From = new MailAddress(SendMailFrom);
                email.To.Add(SendMailTo);
                email.CC.Add(SendMailFrom);
                email.Subject = SendMailSubject;
                email.Body = SendMailBody;
                //END
                SmtpServer.Timeout = 5000;
                SmtpServer.EnableSsl = true;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new NetworkCredential(SendMailFrom, "Google App Password");
                SmtpServer.Send(email);

                Console.WriteLine("Email Successfully Sent");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }

        }
    }
}

Step 2: Create an app password

Firstly, make sure that your two-step verification is on. Now, go to the "Manage your Google Account", then click the "Security" tab.

Google App Password

Now, click the App passwords option. Finally, you can select a Custom name for your app.

Choose Custom Name

An automated password will be created for you, copy that and paste it into your C# code. Make sure you save this password with you, because it will disappear, and you have to then recreate this app password again.

Conclusion:

This is the safest method to use Gmail as your email service, try it and comment if you get it working.👍

If you enjoyed reading this blog post and want to show your support, please consider clicking on the link. Every click helps me to continue creating valuable content for my readers. Thank you for your support!

See you next time,

@TechAE

Buy Me A Coffee

2 comments:

  1. This is exceptionally intriguing substance! I have completely appreciated perusing your focuses and have arrived at the conclusion that you are ideal about a considerable lot of them. You are extraordinary. buy Gmail accounts

    ReplyDelete
  2. Excellent information, Thanks for publishing such essential information. You are doing such a good job. This information is very helpful for everyone. Keep it up. Thanks. Read more info about Bulk gmail accounts

    ReplyDelete

Thank you for submitting your comment! We appreciate your feedback and will review it as soon as possible. Please note that all comments are moderated and may take some time to appear on the site. We ask that you please keep your comments respectful and refrain from using offensive language or making personal attacks. Thank you for contributing to the conversation!

Bottom Ad [Post Page]