我正在尝试使用Gmail API从C#发送一封HTML电子邮件。电子邮件被发送了,但Gmail拒绝承认它应该是HTML电子邮件。
这是我使用的代码:
var template = @"from: {1}{4}to: {0}{4}subject: {2}{4}MIME-Version: 1.0{4}Content-Type: text/html; charset=UTF-8{4}Content-Transfer-Encoding: base64{4}{4}{3}";
body = HttpUtility.HtmlEncode(body);
var result = string.Format(template, to, from, subject, body, "\r\n");
result = Convert.ToBase64String(Encoding.UTF8.GetBytes(result));
var gMessage = new Message()
{
Raw = result
};
service.Users.Messages.Send(gMessage, "me").Execute();
以下是编码为base64之前的结果字符串:
from: test@test.com
to: test@test2.com
subject: testSubject
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
<html><head>
<title>Push Email</title>
</head>
<body> blah
</body></html>
(该应用程序实际上使用的是真实的电子邮件地址,我已经将其替换为"test@...“在上面的示例中用于隐私。)
我尝试了所有可能的标题排列,内容传输编码(base64,7位,8位等),内容类型字符集(ascii,utf8等),我尝试使用UrlEncode而不是HtmlEncode,但电子邮件正文要么显示为未呈现的HTML,要么显示为编码的url字符串(取决于我使用的是html编码还是url编码,以及我指定的内容传输编码)。
关键是,邮件在工作,正文正在发送,但它顽固地拒绝呈现HTML。我要么得到这个:
<html><head> <title>Push Email</title> </head> <body> blah </body></html>
或者这样:
%3chtml%3e%3chead%3e%0d%0a%0d%0a%3ctitle%3ePush+Email%3c%2ftitle%3e+++%0d%0a+%0d%0a%3c%2fhead%3e++++%0d%0a%3cbody%3e+blah++%0d%0a++++%0d%0a%3c%2fbody%3e%3c%2fhtml%3e
或者这样:
<html><head> <title>Push Email</title> </head> <body> blah </body></html>
我只会发送一封SMTP电子邮件,但可能是为了安全起见,谷歌不会允许它,如果你有两个因素的帐户身份验证(我有,不打算禁用)。
此外,我只是将MIME消息构建为常规字符串。这可能与此有关,但我不知道。我不打算使用任何第三方nuget包/库,比如MimeKit。我只想要一个C#解决方案。
最后,我需要能够发送HTML电子邮件,以便我可以发送链接,根据我的应用程序的业务逻辑。
有什么建议吗?
发布于 2018-07-24 18:50:34
我终于明白了。首先,邮件的正文不能转义,但整个MIME字符串应该转义。但是,如前所述,如果我不对正文进行编码,API就会报告一个无效的字节字符串。
问题是生成的base64字符串应该以一种安全的方式编码。Gmail API guide上的python代码使用一个名为urlsafe_b64encode
的方法,该方法与普通的base64方法不同,因为生成的字符串是URL安全的。
我以为我可以用C#复制它,使用超文本标记语言或网址编码,然后使用标准的Convert.ToBase64String
方法将MIME字符串转换为base64,但我错了。在搜索了MSDN网站后,我终于找到了HttpServerUtility.UrlTokenEncode方法,它做了urlsafe_b64encode
python方法所做的事情,那就是将字符串编码成一个URL安全的变体,并将其转换成base64。因此,最终的代码变成:
// Template for the MIME message string (with text/html content type)
var template = @"from: {1}{4}to: {0}{4}subject: {2}{4}MIME-Version: 1.0{4}Content-Type: text/html; charset=UTF-8{4}Content-Transfer-Encoding: base64{4}{4}{3}";
// Fill in MIME message fields
var result = string.Format(template, to, from, subject, body, "\r\n");
// Get the bytes from the string and convert it to a URL safe base64 string
result = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(result));
// Instantiate a Gmail API message and assign it the encoded MIME message
var gMessage = new Message()
{
Raw = result
};
// Use the Gmail API Service to send the email
service.Users.Messages.Send(gMessage, "me").Execute();
发布于 2018-07-24 03:41:12
你的身体看起来像是被HtmlEncode传递了两次,即<
一次传递成为<
两次传递成为&lt;
,这就是你所拥有的。因为你没有在编码之前发布设置body
的原始代码,所以我只能说,在编码到base64之前,你必须将它作为result
字符串:
from: test@test.com
to: test@test2.com
subject: testSubject
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
<html><head>
<title>Push Email</title>
</head>
<body> blah
</body></html>
要获得此信息,您可以替换此行
body = HttpUtility.HtmlEncode(body);
通过这个
body = HttpUtility.HtmlDecode(body);
发布于 2018-07-24 10:43:18
真的只需要选项"IsBodyHtml=true":
using System.Net;
using System.Net.Mail;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var html = "<html><head><body><h1>hello world!</h1></body><head></html>";
var from = "me@here.com";
var to = "them@there.com";
var msg = new MailMessage(from, to)
{
Subject = "My Subject",
Body = html,
IsBodyHtml = true
};
SendGmail("myUserName", "myPassword", msg);
}
public static void SendGmail(string username, string password, MailMessage msg)
{
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(username, password);
client.Send(msg);
}
}
}
https://stackoverflow.com/questions/51485440
复制相似问题