0

Here is some code for sending a test email with the GMail API:

   public bool SendTestEmail(string From, string Subject, string Body)
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.Subject = Subject;
            mail.Body = Body;
            mail.From = new MailAddress(From);
            mail.IsBodyHtml = false;
            mail.To.Add(new MailAddress(From));
            MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mail);

            Message message = new Message();
            message.Raw = Base64UrlEncode(mimeMessage.ToString());

            var result = m_Service.Users.Messages.Send(message, "me").Execute();
        }
        catch (Exception ex)
        {
            SimpleLog.Log(ex);
            return false;
        }

        return true;
    }

It works fine. However, my email settings window in my application caters for other SMTP hosts and as a result has other settings:

Email Settings

Is it possible to feed to the GMail Service:

  • Port number
  • SSL/TLS mode
  • Server Timeout

Or don’t these apply when sending emails like this?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • None of the code you've shown uses the Gmail API, it just uses standard SMTP as far as I can see, unless `m_Service.Users.Messages.Send()` (which we can't see the content of) takes your `MailMessage` (which is an object designed to be used with SMTP mail), gets the data out and puts it into a HTTP request to the API? – ADyson Jun 13 '19 at 16:34
  • 1
    Normally with a MailMessage, the message is sent over SMTP using the SmtpClient class, which [has properties for all the things you mentioned](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8#properties). But yeah if you were sending data to Google via the API, none of them would be relevant because you'd be using HTTP instead of SMTP. Or, if the Gmail API library actually does use SMTP in the background presumably it hard-codes these values since they'll always be the same, as you're always using the same provider (Google) – ADyson Jun 13 '19 at 16:37
  • @ADyson The `m_Service` is an authorised GMail scope .... tied in to my console project. – Andrew Truckle Jun 13 '19 at 16:37
  • Ok. That wasn't at all clear from the question. – ADyson Jun 13 '19 at 16:39
  • @ADyson You can see the scope in my related question: https://stackoverflow.com/questions/56575181/how-to-refresh-a-google-oauth2-accesstoken – Andrew Truckle Jun 13 '19 at 16:40
  • [Gmail Service](https://developers.google.com/gmail/api/quickstart/dotnet) is a wrapper around [Gmail RESTful API](https://developers.google.com/gmail/api/guides/). You should be able to send messages using [SmtpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8) as long as it is enabled on the Gmail account. – Peter Wolf Jun 13 '19 at 16:46
  • 1
    @AndrewTruckle ok well I'd expect it's using the HTTP API, so yeah those settings would be irrelevant. They're only necessary when communicating over SMTP. – ADyson Jun 13 '19 at 16:48

0 Answers0