Creating a message for the Gmail API in C #

I am considering using the Gmail API in the application I'm working on. However, I'm not sure how to change my Java or Python examples to C #. How does an existing sample change?

An example found here.

+6
source share
3 answers

It was a difficult problem to solve, but I still got it. I used the NuGet AE.Net.Mail package to get the RFC 2822 bit.

 using System.IO; using System.Net.Mail; using Google.Apis.Gmail.v1; using Google.Apis.Gmail.v1.Data; public class TestEmail { public void SendIt() { var msg = new AE.Net.Mail.MailMessage { Subject = "Your Subject", Body = "Hello, World, from Gmail API!", From = new MailAddress("[you]@gmail.com") }; msg.To.Add(new MailAddress(" yourbuddy@gmail.com ")); msg.ReplyTo.Add(msg.From); // Bounces without this!! var msgStr = new StringWriter(); msg.Save(msgStr); var gmail = new GmailService(MyOwnGoogleOAuthInitializer); var result = gmail.Users.Messages.Send(new Message { Raw = Base64UrlEncode(msgStr.ToString()) }, "me").Execute(); Console.WriteLine("Message ID {0} sent.", result.Id); } private static string Base64UrlEncode(string input) { var inputBytes = System.Text.Encoding.UTF8.GetBytes(input); // Special "url-safe" base64 encode. return Convert.ToBase64String(inputBytes) .Replace('+', '-') .Replace('/', '_') .Replace("=", ""); } } 

The same code with a little additional analysis and reasons is available here: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

+8
source

Here is what I was able to get using MimeKit .

 public void SendEmail(MyInternalSystemEmailMessage email) { var mailMessage = new System.Net.Mail.MailMessage(); mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress); mailMessage.To.Add(email.ToRecipients); mailMessage.ReplyToList.Add(email.FromAddress); mailMessage.Subject = email.Subject; mailMessage.Body = email.Body; mailMessage.IsBodyHtml = email.IsHtml; foreach (System.Net.Mail.Attachment attachment in email.Attachments) { mailMessage.Attachments.Add(attachment); } var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage); var gmailMessage = new Google.Apis.Gmail.v1.Data.Message { Raw = Encode(mimeMessage.ToString()) }; Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, ServiceEmail); request.Execute(); } public static string Encode(string text) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); return System.Convert.ToBase64String(bytes) .Replace('+', '-') .Replace('/', '_') .Replace("=", ""); } 

@ Eric Gmail Api is missing because it requires consumers to go through several dances to do something relatively basic. For .Net, it should be as simple as populating the Message object (using Subject, From, To, Body, etc.) and passing this object to the Send function, which processes the encoding.

At the very least, the Api documentation should give a complete working example without any third-party libraries such as MimeKit or AE.Net.Mail required.

Note. If you get a problem with email failure, this is probably due to the fact that the ReplyToList field is not set. See: Google API Failures from GMail

+5
source

This doesn't seem to be a Gmail API issue. What you need to look for is “C # create email” (potentially adding “RFC 2822” as the RFC that defines the format of these emails). Once you have such a valid “email message” (real, all mail content that you can use with SMTP or IMAP, etc.), then using it with the Gmail API should be pretty trivial.

+1
source

Source: https://habr.com/ru/post/972154/


All Articles