C# 如何从web.api将Gmail Smtp服务与Oauth2一起使用
我尝试使用GMAIL OAuth协议发送电子邮件,但GoogleWebAuthorizationBroker.AuthorizationAsync方法挂起 我试着从服务器上运行它(它是一个web.api项目,通过htttp POST从客户端接收发送电子邮件信息,然后我的服务应该连接到Google Gmail并发送电子邮件),然后该服务充当Gmail和客户端之间的代理 我从GoogleConsole为Web应用程序获取了客户端机密信息 我使用Visual Studio 2013 C#NET4.5。该服务可以访问我存储数据存储的目录 我使用的代码如下所示,挂起了GoogleWebAuthorizationBroker.AuthorizationAsync DbGoogleDataStore是与Google相同的数据存储实现(我计划将来在数据库中创建我的数据存储,为此我将其分开)C# 如何从web.api将Gmail Smtp服务与Oauth2一起使用,c#,web-services,email,google-app-engine,oauth-2.0,C#,Web Services,Email,Google App Engine,Oauth 2.0,我尝试使用GMAIL OAuth协议发送电子邮件,但GoogleWebAuthorizationBroker.AuthorizationAsync方法挂起 我试着从服务器上运行它(它是一个web.api项目,通过htttp POST从客户端接收发送电子邮件信息,然后我的服务应该连接到Google Gmail并发送电子邮件),然后该服务充当Gmail和客户端之间的代理 我从GoogleConsole为Web应用程序获取了客户端机密信息 我使用Visual Studio 2013 C#NET4.5。
拜托,有人知道怎么做吗 你能显示不工作的代码吗?谢谢你的回答,我添加了使用过的代码。是否返回了任何异常?比如说,超时后?是的“任务被取消”程序在超时后得到异常,第一个异常是“System.AggregateException”,然后内部异常消息是“任务被取消”HResult=-214633029
public void SendMailUsingOauthProtocol(SmtpConfigurationSettings settings, SmtpSendMessageInputModel filter)
{
var msg = new AE.Net.Mail.MailMessage() // MailMessage
{
Subject = filter.Subject,
Body = filter.Body64,
From = new MailAddress(settings.Username),
};
msg.To.Add(new MailAddress(filter.To));
msg.ReplyTo.Add(new MailAddress(filter.To));
var msgStr = new StringWriter();
msg.Save(msgStr);
// Get from settings the Client secret
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(settings.AutClientSecret);
writer.Flush();
stream.Position = 0;
//ClientSecrets secret = GoogleClientSecrets.Load(stream).Secrets;
var storeFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
storeFilePath = storeFilePath + @"\GmailOauth\";
Debug.WriteLine(storeFilePath);
IDataStore fileStore = DbGoogleDataStore.Factory(storeFilePath, true);
// Create the credential.. this step go authenticate with the user if it is necessary
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
stream,
Scopes,
"user",
CancellationToken.None,
fileStore
).Result;
//Create GMAIL API Service
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
var result = service.Users.Messages.Send(new Message { Raw = Base64UrlEncode(msgStr.ToString()) }, "me").Execute();
}