c# - How to create a Gmail API Message -


i'd send message using google's gmail api. i've authenticated successfully, , trying use gmailservice send message.

i'd use this:

myservice.users.messages.send(mymessage, "me").execute(); 

where myservice google.apis.gmail.v1.gmailservice , mymessage google.apis.gmail.v1.data.message.

myservice fine, i've done oauth dance. can messages inbox , that. don't know how construct mymessage. have standard .net mailmessage, human-readable subject, body, to, etc.

but google message class takes fields payload or raw. what's easiest way convert full mailmessage string can set payload or raw properties? or not should doing @ all?

the documentation message class.

i found solution. strangely, .net doesn't seem support natively/easily. there's nice nuget package though, called ae.net.mail, can write easy-to-create message object stream.

here's sample code pointed me in direction.

copy-and-pasted code site seems down, , google's cache might not last forever:

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, gmail api!",       = 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(context.googleoauthinitializer);     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("=", "");   } } 

Comments