gpt4 book ai didi

Send Email To Admin(向管理员发送电子邮件)

转载 作者:bug小助手 更新时间:2023-10-28 14:01:29 26 4
gpt4 key购买 nike



I need to send a lot of different forms to the mail. For example FeedBackForm ContactsForm. the only difference is that I create different mail.Subject and mail.Body.

我需要寄很多不同的表格去邮寄。例如,FeedBackForm ContactsForm。唯一的区别是我创建了不同的mail.Subject和mail.Body。


I need to automate this action.

我需要自动执行此操作。


public class EmailService : IEmailService, ITransientService
{
private IConfiguration _configuration;

public EmailService(IConfiguration configuration)
{
_configuration = configuration;
}

public async Task SendFormToAdminEmailAsync (FeedBackForm form)
{
var fromAddress = _configuration["EmailCredentials:Email"];
var fromPassword = _configuration["EmailCredentials:Password"];

var toAddress = new MailAddress("[email protected]");

var mail = new MailMessage(new MailAddress(fromAddress), toAddress);

mail.Subject = $"Need consultation {form.Name}";
mail.Body = $@"Name: {form.Name}
Email: {form.Email}
Phone: {form.PhoneNumber}
Link: {form.BotLink}";

using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", fromPassword);
smtp.EnableSsl = true;

await smtp.SendMailAsync(mail);
}
}
}

I tried this, in this method I accepted a generic type, and with the help of automapper I mapped what I needed, and specified the conditions in MappingConfig, but I don’t know if this is correct

我试过了,在这个方法中,我接受了一个泛型类型,在自动映射程序的帮助下,我映射了我需要的东西,并在MappingConfig中指定了条件,但我不知道这是否正确


更多回答

Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.

请澄清您的具体问题或提供更多详细信息,以突出您的确切需求。按照目前的写法,很难准确地说出你在问什么。

优秀答案推荐

The way I'd proceed would be to (if you don't already have it) create an interface for forms (FeedbackForm, ContactForm)

我继续的方法是(如果您还没有)为表单(Feedback Form、ContactForm)创建一个界面


interface IFormData
{
// Don't know if you have some common ground on forms
// This may be an empty interface to mark types that can be worked with
// in later stages of this solution.
}

You can create an interface like:

您可以创建如下界面:


interface IMailHydrator
{
void Hydrate(MailMessage message, IFormData form);
}

And than you create implementation like

然后您创建如下实现


class FeedBackFormHydrator : IMailHydrator
{
public void Hydrate(MailMessage message, IFormData form)
{
var cast = form as FeedBackForm;
if (cast == null)
{
throw new InvalidOperationException();
}

message.Subject = $"Need consultation {cast.Name}";
mail.Body = $@"Name: {cast.Name}
Email: {cast.Email}
Phone: {cast.PhoneNumber}
Link: {cast.BotLink}";
}
}

You register those hydrators in your DI as named/keyed registrations where the name/key is full name of form type. So something like:

您在DI中将这些加水器注册为命名/键控注册,其中名称/键是表单类型的全名。所以大概是这样:


this.AddSingleton<IMailHydrator, FeedBackFormHydrator>().Named(typeof(FeedBackForm).FullName);

You change your method to:

您可以将您的方法更改为:


public async Task SendFormToAdminEmailAsync (IFormData form)
{
var fromAddress = _configuration["EmailCredentials:Email"];
var fromPassword = _configuration["EmailCredentials:Password"];
var toAddress = new MailAddress("[email protected]");
var mail = new MailMessage(new MailAddress(fromAddress), toAddress);

var hydrator = this.container.Resolve<IMailHydrator>(form.GetType().FullName);
hydrator.Hydrate(mail, form);
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", fromPassword);
smtp.EnableSsl = true;

await smtp.SendMailAsync(mail);
}
}

There may be some tweaking depending on your DI container.

根据您的DI容器的不同,可能会有一些调整。


更多回答

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