控制器中如何调用自动发送邮件的方法?

问题描述 投票:0回答:1

我需要能够向任何注册新帐户、更改密码和/或创建新订单的用户发送自动电子邮件。

我已获得 SendEmail 文件,该文件位于我的解决方案中的“实用程序”文件夹中。

using System;
using System.Net.Mail;
using System.Net;

namespace SendEmail

{
public static class EmailMessaging
{
    public static void SendEmail(String toEmailAddress, String emailSubject, String emailBody)
    {
      
        //Create a variable for YOUR TEAM'S Email address
        //This is the address that will be SENDING the emails (the FROM address)
        String strFromEmailAddress = "[email protected]";

        //This is the password for YOUR TEAM'S "fake" Gmail account
        String strPassword = "Password";

        //This is the name of the business from which you are sending
        //TODO: Change this to the name of the company you are creating the website for
        String strCompanyName = "Team Final Project";

        //Create an email client to send the emails
        //port 587 is required to work, do not change it
        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            UseDefaultCredentials = false,
            //This is the SENDING email address and password
            //This will be your team's email address and password
            Credentials = new NetworkCredential(strFromEmailAddress, strPassword),
            EnableSsl = true
        };


        //Add anything that you need to the body of the message
        //emailBody is passed into the method as a parameter
        // /n is a new line – this will add some white space after the main body of the message
        //TODO: Change or remove the disclaimer below
        String finalMessage = emailBody + "\n\n Thank you, come back again soon!";

        //Create an email address object for the sender address
        MailAddress senderEmail = new MailAddress(strFromEmailAddress, strCompanyName);

        //Create a new mail message
        MailMessage mm = new MailMessage();

        //Set the subject line of the message (including your team number)
        mm.Subject = "Team ## - " + "Thank you!";

        //Set the sender address
        mm.Sender = senderEmail;

        //Set the from address
        mm.From = senderEmail;

        //Add the recipient (passed in as a parameter) to the list of people receiving the email
        mm.To.Add(new MailAddress(toEmailAddress));

        //Add the message (passed)
        mm.Body = finalMessage;

        //send the message!
        client.Send(mm);
    }
  }
}

我的问题是,我和我的团队成员都不知道如何以自动发送用户电子邮件和姓名的方式从控制器实现调用。我们想象它们将位于帐户和订单控制器中。帐户控制器具有注册和更改密码方法,可以使用,并且订单控制器具有完整的订单方法。

此外,我们使用确认视图,它必须是自动电子邮件。

我们需要一些指导来确定我们到底需要从哪里调用该方法以及如何调用该方法。

我今天在互联网上发现的最有用的东西是这段用于测试消息的代码,该消息无意发送自动电子邮件。

public static void CreateTestMessage(string server)
        {
        MailAddress from = new MailAddress("[email protected]", "Team Project");
        MailAddress to = new MailAddress("[email protected]", "Customer");
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Password Changed";
        message.Body = @"This is a confirmation email that your password has been changed.";
        SmtpClient client = new SmtpClient(server);
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        
        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
                ex.ToString());
        }
    }

一切都在 MS VS 上编码

c# visual-studio asp.net-core email smtpclient
1个回答
0
投票

首先为EmailService创建一个服务,并将SendEmailAsync方法放入其中 并在您的控制器上调用此方法。

在方法体中:
1-创建您的消息:

var mm = new MailMessage()

2-那么你应该构建你的 smtp:

using  var smtpClient = new SmtpClient();

3-然后将其连接到您的服务器

await smtpClient.ConnectAsync(
                "your host",
                "port",
                SecureSocketOptions.SslOnConnect);

4-现在发送您的电子邮件:

await smtpClient.SendAsync(mm);

5-确保与客户端断开连接:

await smtpClient.DisconnectAsync(true);

注意:连接或发送电子邮件时可能会出现异常,因此不要忘记 try catch 块。

6-对于自动化,您可以使用与您的 Customer 表相关的 EmailAccount 表,并将消息数据保存在其中。 例如:身体,主题.......

在您的控制器中,您有您的客户,因此您可以从 DataBase 获取他的 EmailAccount 并将 EmailAccount 实体传递给 SendEmailAsync 方法。

不要在正文中创建消息,而是从 EmailAccount 实体获取消息,然后计算步骤。

7-享受它:)

© www.soinside.com 2019 - 2024. All rights reserved.