IMG-LOGO

How to setup App passwords for Google to send email?

andy - 01 Feb, 2023 768 Views 0 Comment

If you need a static or fixed password to send an email from your Google email you can now use the App Passwords option. This will be handy especially if you have a site or mobile app that you want to integrate with Google Email.

The first thing you have to do is to go Google My Account page.

Go to My Google Account Page

There is a menu on the left hand side called Security. Click this link menu. Then click on the 2-Step Verification link. There is a section called App Password. Click this section.

Then you click the "App Passwords" on the right panel.

A popup menu will then appear to ask you what kind of app password you want to set up. In this example, we want to choose Mail for the app and for the device option we can select other and we can specify the free text as a website.

Once you have specified all information required, you can generate the password. Here is a sample of how the password is generated. Note: the screenshot attached below has blurred parts of the password.

In this example, we are going to use the C# .Net to send an email. Here is the sample code of the function.

public static string SendMail(string host, int port , string username, string password, bool enableSSL, string emailName, string emailFrom, string emailTo, string cc, string bcc, string subject, string body)
{
	bool isHTML = true;
	//declare objects
	MailMessage message = new MailMessage();
	SmtpClient smtp = new SmtpClient(host, port);

	//add try exception
	try
	{
		//Add email address, cc

		message.From = new MailAddress(emailFrom, emailName);
		message.To.Add(new MailAddress(emailTo));
		if (cc != string.Empty)
		{
			message.CC.Add(new MailAddress(cc));
		}

		if (bcc != string.Empty)
		{
			message.Bcc.Add(new MailAddress(bcc));
		}

		//Add subject, body and attachment
		message.Subject = subject;
		message.Body = body;

		//Email Authentication
		if (username.Trim() != string.Empty && password.Trim() != string.Empty)
		{
			NetworkCredential basicAuthentication = new NetworkCredential(username, password);
			smtp.UseDefaultCredentials = false;
			smtp.Credentials = basicAuthentication;
		}

		//enable SSL
		smtp.EnableSsl = enableSSL;

		//smtp port
		if (port > 0) { smtp.Port = port; }

		//check if html format is needed
		if (isHTML) { message.IsBodyHtml = true; } else { message.IsBodyHtml = false; }

		//Send the email
		if (emailFrom.Length > 0 && body.Length > 0 && subject.Length > 0)
		{
			smtp.Send(message);
		}
	}
	catch (Exception ex)
	{
		return "Error sending email to : " + emailTo + ". Email Subject: " + subject + ". Details: " + ex.Message + ". Inner exception: " + ex.InnerException;
	}
	return "";
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

Google Analytics API Tutorial using JavaScript

In this tutorial you will learn how to access Google Analytics API using javascript We will use Oauth2 to access user account analytics and a combination of Google Analytics version 3 to get the list of account summaries and version ...