Search code examples
google-apigmailgmail-api

gmail api vs traditional smtp


In the Java world

Wonder if any one has information on comparisons of sending emails via gmail api vs configuration of gmail smtp service configured via java mail service.

Pros / cons of either

Bulk emails can one connection trigger 5 separate emails ?


Solution

  • The main difference between using SMTP / IMAP vs Gmail API. IMO would be the way you login. SMTP and IMAP allow you to authentication using client login (login and password) while Gmail API is going to require that you use Open authentication (Oauth2) to be exact.

    Now are there any drawbacks to that. Well up until about six months ago I would have said yes if the user changed their password your SMTP solution would not work anymore while your Oauth solution would. However Google recently made a change that if the user changes there password and you are authentication using the mail scope then all refresh tokens will expire. So now if the user changes their password neither solution will work until the user updates the password in the application.

    Quick fix for SMTP username and password not accepted error

    Lets talk about scopes

    When you authentication using Oauth2 you have to request the scope of the access you are requesting. There are a bunch of email scopes you can find them all here. Lets just look at two.

    https://www.googleapis.com/auth/gmail.readonly Read all resources and their metadata—no write operations.
    https://www.googleapis.com/auth/gmail.compose Create, read, update, and delete drafts. Send messages and drafts.

    Now users tend to like there privacy and don't often like giving out permissions that applications don't need. If the purpose of your application is to just display the users emails then you wouldn't need write access would you? So some users would refuse to run your application if you requested write access and didn't need it.

    So using the Gmail API with Oauth will allow you to only request the scope of access you need.

    Now SMTP and IMAP are talking directly to the mail server there is really no way to set the amount of access you are granted. You have full access to the account.

    Libraries

    I am not a Java dev I am actually a .net dev. That being said I think you may want to consider the libraries available and how easy they are to use. The Official Google Java client library is probably very similar to the Google .Net client library and its probably well tested for use with Gmail and stable. There are probably a number of tutorials out there on how to use it as well.

    Now you are going to have to find an SMTP or IMAP library and figure out how that will work with the GMail mail server. Then you will have to see if you can find tutorials on it as well.

    Batching

    Most of the Google APIs support the batching endpoint. Top tip: when testing with this use a dummy email account if you get it banned you don't want to lock out your personal gmail account.

    I am sure that SMTP servers support batching somehow.

    Quota

    Quota is the number of requests you can make against the API per day.

    enter image description here

    I am not aware of any Quotas as far as SMTP goes.

    Note:

    You can use OAuth with the SMTP server the documentation on it is here I haven't had time to play with it myself yet but it looks fun. So I cant comment on the value of that.