簡體   English   中英

如何調用一個方法在 controller 中自動發送一個 email?

[英]How do I call a method to automatically send an email in the controller?

我需要能夠向注冊新帳戶、更改密碼和/或創建新訂單的任何用戶發送自動電子郵件。

我已獲得 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@gmail.com";

        //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);
    }
  }
}

我的問題是,我和我的團隊成員都不知道如何以自動發送的方式從 controller 執行調用,並使用用戶的 email 和姓名。 我們想象它們將在帳戶和訂單控制器中。 賬戶controller有注冊和修改密碼的方法,有效,訂單controller有完整的下單方法。

此外,我們沒有使用確認視圖,它必須是自動 email。

我們需要一些指導來確定我們需要從哪里調用該方法以及如何調用該方法。

我今天在 inte.net 上發現的最有用的東西是這段代碼,用於測試消息,它不打算發送自動電子郵件。

public static void CreateTestMessage(string server)
        {
        MailAddress from = new MailAddress("sender@gmail.com", "Team Project");
        MailAddress to = new MailAddress("reciever@gmail.com", "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 上編碼

首先為 EmailService 創建一個服務,並在其中放入 SendEmailAsync 方法,然后在您的 Controller 上調用此方法。

在方法體中:
1-創建您的消息:

var mm = new MailMessage()

2-然后你應該建立你的 smtp:

using  var smtpClient = new SmtpClient();

3-然后將其連接到您的服務器

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

4-現在發送您的 Email:

await smtpClient.SendAsync(mm);

5-確保斷開與客戶端的連接:

await smtpClient.DisconnectAsync(true);

注意:它可能會在連接或發送 Email 時給您一個異常,所以不要忘記 try catch 塊。

6-對於自動化,您可以使用與您的客戶表相關的電子郵件帳戶表,並將您的消息數據保存在其中。 例如:身體,主題.......

在您的 Contorller 中,您有您的客戶,因此您可以從數據庫中獲取他的EmailAccount ,並將EmailAccount實體傳遞給SendEmailAsync方法。

不要在正文中創建消息,而是從EmailAccount實體獲取它,然后繼續執行步驟。

7-享受它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM