ASP.NETCORE-发送电子邮件:请通过您的web浏览器登录,然后重试
创始人
2024-09-17 08:00:09
0次

出现这个错误是由于ASP.NET CORE应用程序中使用了OAuth2身份验证。要解决此问题,需要更改应用程序设置以允许此操作,并获取正确的OAuth2令牌。

以下是一个示例,展示如何使用Gmail帐户发送电子邮件:

1.在Startup.cs文件中,将你的电子邮件设置添加到ConfigureServices方法中:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddGoogle(options =>
{
    options.ClientId = Configuration["Authentication:Google:ClientId"];
    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
services.AddTransient();

2.在应用程序中创建一个EmailSender.cs文件,在其中添加发送电子邮件的方法:

using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

public class EmailSender
{
    private readonly GmailService _gmailService;
    private readonly IConfiguration _configuration;
    private readonly IHostingEnvironment _hostingEnvironment;

    public EmailSender(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
    {
        _configuration = configuration;
        _hostingEnvironment = hostingEnvironment;
        var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
        {
            ClientId = _configuration["Authentication:Google:ClientId"],
            ClientSecret = _configuration["Authentication:Google:ClientSecret"]
        },
        new[] { GmailService.Scope.GmailSend }, "user", CancellationToken.None, new FileDataStore(_hostingEnvironment.WebRootPath)).Result;
        _gmailService = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = _configuration["Authentication:Google:ApplicationName"],
        });
     }

    public async Task SendMessageAsync(string from, string to, string subject, string

相关内容

热门资讯

WPK脚本要领!德扑之星透视辅... WPK脚本要领!德扑之星透视辅助软件是真的吗,微扑克ai多功能辅助器技巧一、德扑之星透视辅助软件是真...
WPK靠谱教程书!微扑克辅助,... WPK靠谱教程书!微扑克辅助,红龙PoKerai多功能透视软件1、在微扑克辅助插件功能辅助器技巧中,...
WPK安装项目!aa poke... 您好,aa poker辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
WPK辅助技法!werplan... WPK辅助技法!werplan辅助软件,德扑之星ai多功能透视挂教程小薇(辅助器软件下载)致您一封信...
WPK安装妙计!werplan... WPK安装妙计!werplan辅助软件,德州扑克ai多功能辅助器技巧所有人都在同一条线上,像星星一样...
WPK真假手筋!智星德州插件,... WPK真假手筋!智星德州插件,aapokerai多功能辅助app暗藏猫腻,小编详细说明智星德州插件破...
WPK模拟器模块!AApoke... WPK模拟器模块!AApoker脚本,WePoKerai多功能透视脚本1、WPK模拟器模块!AApo...
WPK多开法门!aapoker... WPK多开法门!aapoker能控制牌吗,红龙PoKerai多功能辅助器攻略进入游戏-大厅左侧-新手...
WPK模拟器妙计!德普之星辅助... WPK模拟器妙计!德普之星辅助器怎么用,aapokerai多功能透视app1、德普之星辅助器怎么用有...
WPK靠谱课程!wpk作弊是真... WPK靠谱课程!wpk作弊是真的吗,菠萝智星德州ai多功能辅助器技巧1、wpk作弊是真的吗免费脚本咨...