« Merge PDFs with page … | Home | Four month Russian wa… »

Sending email via gmail

Recently a client had questions on how to send an email using our CURLEmailMBS class via gmail service and our CURLSMBS class. There are a few challenges:

  1. You need to know what server to use. That is easy for google mail and smtp.gmail.com will do it.
  2. You need to know your login credentials. Your account should have 2 Factor Authentication. But you never pass your gmail password to an application directly. You go to the google website, login and then create an app-specific password for smtp. The advantage is that you can disable that password later, if it got stolen and create a new one.
  3. You need to know the port to use. Basically port 25, 465 and 587 all can work. But 25 and 587 connect in plain text and do the TLS upgrade later after discussing the details of the supported encryption variants. For port 465, the first package is encrypted and for that we would tell the plugin to use TLS directly. That is our UseSSL flag, you can set when using port 465. To set the port, please either put in the server, e.g. "smtp.gmail.com:587" or later set it on the CURL object with OptionPort. For gmail the default port 25 works just fine here.
  4. You need to know what TLS version is allowed. For our example we don't need to set an explicit version. CURL now tries TLS 1.3 and falls back to version 1.2 if the server doesn't support it. But you can set a version as minimum.
  5. Since gmail doesn't do plain text, we tell CURL to require SSL with OptionUseSSL = kUseSSLall.

When everything is setup correctly, the following code should send an email just fine:

Sub SendEmail() // send email via gmail Dim email As New CURLEmailMBS email.SetFrom "sender@monkeybreadsoftware.com", "John Müller" email.AddTo "receiver@monkeybreadsoftware.de", "Christian Schmitz" email.SMTPUsername = "monkeybreadsoftware@gmail.com" // gmail email as account email.SMTPServer = "smtp.gmail.com" email.SMTPPassword = "eotkfwhxnkfayba" // app-specific password, not your gmail login password! // Content of the Email email.Subject = "Hello World via gmail" email.PlainText = "Hello World," + EndOfLine + _ "" + EndOfLine + _ "this Is just a test email With plain Text. 😄" + EndOfLine + _ "" + EndOfLine + _ "Greetings" + EndOfLine + _ "Christian" // add an attachment dim pic as Picture = LogoMBS(500) Dim jpegData As String = Pic.GetData(pic.FormatJPEG) email.AddAttachment jpegData, "mbs.jpg", "image/jpeg" Dim curl As New CURLSMBS If Not curl.SetupEmail(email) Then // problem? Dim ee As Integer = curl.Lasterror Break End If curl.OptionUseSSL = curl.kUseSSLall 'curl.OptionSSLVersion = curl.kSSLVersionTLSv12 Dim error As Integer = curl.Perform If error = 0 Then MessageBox "Mail sent." Else MessageBox "Failed with error "+Str(error)+": "+curl.LasterrorText Dim DebugLog As String = curl.DebugData Break // read log! End If End Sub

Let us know if you have questions. We use the mail server from our web hoster for sending out all our emails. For newsletter we got an extra account to send without a limiting hourly capacity limit. And for you it may be worth to get an email server at your hoster with your own domain, so your emails have your own branding and not the gmail email addresss.

Please don't hesitate to contact us with questions.

The biggest plugin in space...
22 06 22 - 11:23