I am adapting an application to send out emails through GMail and I thought I might use the Google API to do it. I have the API library from here; https://github.com/googleapis/google-api-dotnet-client
The Github repo and linked developers.google.com and googleapis.dev pages dont really have a lot of good examples of the usage of the API, at least for Gmail so far as I can see.
I have a message creating method:
private Message CreateCustomerMessage()
{
var customerBody = GenerateCustomerMessageBody();
var customerMessage = new Message
{
Payload = new MessagePart
{
Headers = new List<MessagePartHeader>() {
new MessagePartHeader() { Name = "From", Value = "\"Reservation Service\" <reservations@domain.com>"},
new MessagePartHeader() { Name = "To", Value = Reservation.PartyEmail },
new MessagePartHeader() { Name = "Content-type", Value = "text/html; charset=iso-8859-1" }
},
Body = new MessagePartBody
{
Data = customerBody.ToBase64Url()
},
Parts = new List<MessagePart>()
{
new MessagePart()
{
Filename = "Trip_Calendar.ics",
Body = new MessagePartBody(){ Data = CreateShuttleCalendarEvent(customerBody).ToBase64Url() },
Headers = new List<MessagePartHeader>()
{
new MessagePartHeader() { Name = "Content-type", Value = "text/calendar"}
}
}
}
}
};
return customerMessage;
}
I am creating the body in a separate method call, creating the Message with a Payload wherein the Body is set to the Base64Url encoded body generated above. Also setting headers and so forth.
Additionally I am creating a calendar file and attaching it.
Upon passing the message to the Send() method, I get the following exception message:
The service gmail has thrown an exception. HttpStatusCode is BadRequest. 'raw' RFC822 payload message string or uploading message via /upload/* URL required
I had hoped that the issue was just with the calendar file, but removing that and sending returned the same exception.
I have seen posts saying that you can construct the raw email message complete with all of the headers in a string and attach that to the message instead of constructing it with the MessageParts and whatnot. I was hoping to use this API library the way it was intended to be used, but if I have to do it with the raw message I suppose I will. Are there any good examples that anyone might know of showing how these Google API Library classes are supposed to be used? Personally the above method looks nice and clean and does not rely on a large string interpolation with injected values.
Edit
I do not consider this a duplicate of the above linked post. The above posts answers create the message with System.Net.Mail.MailMessage and AE.Net.Mail.MailMessage rather than using the API supplied classes. My post was specifically asking about how the API Library was meant to be used. Those classes are there for a reason. Presumably they are meant to be used to send mail. Why aren't they? Are they broken functionality when sending? Is the expected method to use some other class to construct the mail message then output the raw and attach it to the Gmail Message class instance? That seems counter intuitive to me. I would appreciate my post being restored and no longer marked as duplicate.