简体   繁体   English

使用 ASP.NET 内核 6.0 Web ZDB974238714CA8DE634A7CE1D08 发送 email

[英]Sending email using ASP.NET Core 6.0 Web API

I am trying to send a email from an ASP.NET Core 6 Web API using gmail account sampleEmail@gmail.com . I am trying to send a email from an ASP.NET Core 6 Web API using gmail account sampleEmail@gmail.com . I noticed that the option to allow less secure apps was no longer available on gmail and whenever I try sending the email it does nothing, but no errors are thrown.我注意到在 gmail 上不再提供允许不太安全的应用程序的选项,每当我尝试发送 email 时它什么都不做,但不会引发错误。

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyPortfolioWebAPI.Data;
using MyPortfolioWebAPI.Models;
using Microsoft.AspNetCore.Cors;
using System.Text;
using MimeKit;
using MimeKit.Text;
using MailKit.Net.Smtp;
using MailKit.Security;

try
{
    StringBuilder template = new();
    template.AppendLine("from  " + email.EmailAddress);
    template.AppendLine("Name  " + email.Name);
    template.AppendLine(email.Message);

    var Mails = new MimeMessage();
    Mails.From.Add(MailboxAddress.Parse("sampleEmail@gmail.com"));
    Mails.To.Add(MailboxAddress.Parse("thabisofakude40@gmail.com"));
    Mails.Subject = " test response ";
    Mails.Body = new TextPart(TextFormat.Text) { Text = template.ToString() };

    using var smtp = new SmtpClient();
    smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate("sampleEmail@gmail.com", "Password");

    smtp.Send(Mails);

    smtp.Disconnect(true);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

Kindly assist请协助

i got it i just had to enable 2 factor authentification on gmail我明白了,我只需要在 gmail 上启用 2 因素身份验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AllowAnonymous 不适用于 ASP.NET 内核 6.0 Web API - AllowAnonymous not working with ASP.NET Core 6.0 Web API 使用 ASP.NET Core 6.0 中的中间件将 Auth0 用户 ID 传递到服务层 Web API - Passing Auth0 User Id to service layer using middelware in an ASP.NET Core 6.0 Web API 确认 Email 在 asp.net 内核 web api - Confirm Email in asp.net core web api 使用 Azure AD 验证返回 401 - Sending a GET request to an ASP.NET Core Web Application API using Azure AD Authentication returns 401 错误消息不会使用 asp.net core 1.0.1 web api 应用程序发送到 Rollbar 仪表板 - Error messages are not sending to Rollbar dashboard using asp.net core 1.0.1 web api application ASP.NET 内核 Web API - 自定义唯一 Email 验证不工作 DatanotValidation - ASP.NET Core Web API - Custom Unique Email Validation using Data Annotation not working ASP.NET Core 2.2 Web API 项目:使用端点实现电子邮件确认回调 url - ASP.NET Core 2.2 Web API project: implement email confirmation callback url using endpoint 创建新的 ASP.NET Core 6.0 Web API 时出现问题 - Issue when creating a new ASP.NET Core 6.0 Web API 如何在ASP.NET Core 6.0 Web API项目中启用cors? - How to enable cors in ASP.NET Core 6.0 Web API project? ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM