gpt4 book ai didi

c# - Blazor C# 发送电子邮件

转载 作者:行者123 更新时间:2023-12-05 00:46:30 25 4
gpt4 key购买 nike

我的 Blazor 项目中有一个 razor 组件,当用户单击按钮时,它将触发电子邮件 The button

OnClick 函数 Email 将通过以下代码发送电子邮件

async Task Email(string originalpiv, string docno, string cuscode, string cusname, string docdate)
{
CModule.SendMail("example@gmail.com", "example@gmail.com", "", "HI", "HI BRO");

}

CModule.SendMail 是传递电子邮件 from,to,subj,body 的方法,代码如下

public static bool SendMail(string from, string to, string cc, string subject, string body)
{
bool rst = false;
MailMessage Msg = new MailMessage();
try
{
SmtpClient smtp = new SmtpClient("mail.gmail.com", 587);
string[] toeml = to.Split(new Char[] { ';', ',' });
foreach (string tmp in toeml)
{
if (tmp.Trim() != "") Msg.To.Add(tmp.Trim());
}
string[] cceml = cc.Split(new Char[] { ';', ',' });
foreach (string tmp in cceml)
{
if (tmp.Trim() != "") Msg.CC.Add(tmp.Trim());
}
Msg.From = new MailAddress(from.Trim());
Msg.Subject = subject;
Msg.Body = body;
Msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
smtp.Credentials = new System.Net.NetworkCredential("nick.chau@gmail.com", "880215");
smtp.Send(Msg);
rst = true;
}
catch (Exception ex)
{

}
finally
{
Msg.Dispose();
}
return rst;
}

这些是我尝试过的代码,但电子邮件没有发送出去,不知道为什么,有什么想法吗?

最佳答案

您是否将电子邮件类添加为服务以用作 startup.cs 中的依赖注入(inject)?

这对我有用:

  1. 创建将通过 appsettings.json 配置的电子邮件设置
namespace ReservationBookingSystem.Model
{
public class MailSettings
{
public string Username { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public string FromEmail { get; set; }
public string Host { get; set; }
}
}
  1. 创建邮件界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ReservationBookingSystem.Model;

namespace ReservationBookingSystem.Services
{
public interface IMailService
{
Task SendEmailAsync(string ToEmail, string Subject, string HTMLBody);
}
}

  1. 创建邮件服务类
using ReservationBookingSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;

namespace ReservationBookingSystem.Services
{
public class MailService : IMailService
{
private readonly MailSettings _mailConfig;
public MailService(MailSettings mailConfig)
{
_mailConfig = mailConfig;
}

public async Task SendEmailAsync(string ToEmail, string Subject, string HTMLBody)
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(_mailConfig.FromEmail);
message.To.Add(new MailAddress(ToEmail));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = HTMLBody;
smtp.Port = _mailConfig.Port;
smtp.Host = _mailConfig.Host;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(_mailConfig.Username, _mailConfig.Password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
await smtp.SendMailAsync(message);
}
}
}

  1. 在 Startup.cs 添加:
services.AddSingleton(Configuration.GetSection("MailSettings").Get<MailSettings>());
services.AddScoped<IMailService, MailService>();
  1. 在 appsettings.json 中的“MailSettings”下添加邮件配置
  2. 在你想使用的razor组件中,调用依赖注入(inject)即可。使用 @inject ReservationBookingSystem.Services.IMailService MailService。代码内部调用 await MailService.SendEmailAsync("to.email@mail.com", "test", "test");

这适用于 blazor 服务器。所以基本上对你来说,用 CModule 替换 MailSettings 就可以了

关于c# - Blazor C# 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60111792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com