我尝试发送基于Gmail API的电子邮件。我在Google Develover Console中创建了一个项目。 我提到了GmailService实例:https://developers.google.com/gmail/api/quickstart/dotnet
以下是发送电子邮件的代码:
var msg = new AE.Net.Mail.MailMessage { Subject = "Your Subject", Body = "Hello, World, from Gmail API!", From = new MailAddress("[you]@qq.com") }; msg.To.Add(new MailAddress("yourbuddy@qq.com"));
msg.ReplyTo.Add(msg.From); // Bounces without this!!
var msgStr = new StringWriter();
msg.Save(msgStr);
string[] Scopes = { GmailService.Scope.GmailSend };
string ApplicationName = "Gmail API .NET Quickstart";
UserCredential credential
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
var gmail = new GmailService(new Google.Apis.Services.BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, });
var result = gmail.Users.Messages.Send(new Google.Apis.Gmail.v1.Data.Message { Raw = Base64UrlEncode(msgStr.ToString()) }, "user").Execute();
类型' Google.GoogleApiException'未处理的例外情况发生在 Google.Apis.dll其他信息: Google.Apis.Requests.RequestError错误请求[400] 错误[ 消息[错误请求]位置[ - ]原因[failedPrecondition] 域[全球] ]
知道出了什么问题吗?谢谢!