I'd like to send a Message using Google's Gmail API. I've authenticated successfully, and am trying to use GmailService to send a message.
我想用谷歌的Gmail API发送一条消息。我已经验证成功,并且正在尝试使用GmailService发送消息。
I'd like to use this:
我想用这个:
myService.Users.Messages.Send(myMessage, "me").Execute();
where myService is a Google.Apis.Gmail.v1.GmailService
and myMessage is a Google.Apis.Gmail.v1.Data.Message
.
myService是google。api。gmail1。GmailService和myMessage是google . api . gmail.v1 . data . message。
myService
is fine, I've done the OAuth dance. I can get messages from my Inbox and all that. But I don't know how to construct myMessage
. I have a standard .NET MailMessage
, with human-readable Subject, Body, To, From etc.
我的服务很好,我跳过舞。我可以从我的收件箱里收到信息等等。但我不知道如何构建我的消息。我有一个标准的。net邮件消息,包含人类可读的主题、正文、To、From等。
But the Google Message
class takes fields Payload
or Raw
. What's the easiest way to convert a full MailMessage
to a string which I can set to the Payload
or Raw
properties? Or is this not what I should be doing at all?
但是谷歌消息类接受字段有效载荷或原始数据。将完整邮件消息转换为字符串的最简单方法是什么?我可以将字符串设置为有效负载或原始属性。或者这不是我应该做的吗?
The documentation for the Message class.
消息类的文档。
2 个解决方案
#1
7
I found a solution. Strangely, .NET doesn't seem to support this natively/easily. There's a nice nuget package though, called AE.Net.Mail, which can write an easy-to-create message object to a stream.
我找到了一个解决方案。奇怪的是,. net似乎并不能很容易地支持这一点。不过有一个不错的nuget包,叫做AE.Net。邮件,它可以将一个易于创建的消息对象写入流。
Here's the sample code that pointed me in that direction.
这是指向这个方向的示例代码。
Copy-and-pasted code as site seems to be down, and 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, 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(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("=", "");
}
}
#2
0
Here is an alternative version using using MimeKit.
这里有一个使用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("=", "");
}
Note: If you are getting an email bounce issue, it is likely due to not setting the ReplyToList field. See: GMail API Emails Bouncing
注意:如果你收到邮件被退回的问题,很可能是因为没有设置回复列表字段。参见:GMail API邮件的跳转
#1
7
I found a solution. Strangely, .NET doesn't seem to support this natively/easily. There's a nice nuget package though, called AE.Net.Mail, which can write an easy-to-create message object to a stream.
我找到了一个解决方案。奇怪的是,. net似乎并不能很容易地支持这一点。不过有一个不错的nuget包,叫做AE.Net。邮件,它可以将一个易于创建的消息对象写入流。
Here's the sample code that pointed me in that direction.
这是指向这个方向的示例代码。
Copy-and-pasted code as site seems to be down, and 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, 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(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("=", "");
}
}
#2
0
Here is an alternative version using using MimeKit.
这里有一个使用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("=", "");
}
Note: If you are getting an email bounce issue, it is likely due to not setting the ReplyToList field. See: GMail API Emails Bouncing
注意:如果你收到邮件被退回的问题,很可能是因为没有设置回复列表字段。参见:GMail API邮件的跳转