简体   繁体   English

如何将 email 动态发送给正在使用 C# 中的应用程序的用户?

[英]How to send the email dynamically to the user who is using the application in C#?

This is my code which send email when a process is submitted but the email address are hardcoded now.这是我的代码,它在提交进程时发送 email 但 email 地址现在是硬编码的。 I want to send the email to the user who has logged in and using the application and to admin.我想将 email 发送给已登录并使用该应用程序的用户和管理员。 how can I change the hardcoded email address to a dynamic one如何将硬编码的 email 地址更改为动态地址

public void SendFinalStatusMail(Guid fileGuid)
{
    PulseTest("Acknowledgement|SendFinalStatusMail| Prepare Mail Content started");

    List<string> toEmailAddresses = new List<string>() { "abc@gmail.com", "bdc@gmail.com", "def@gmail.com" }; //dynamic list for recipients

    string toCCEAddress = String.Empty;
    string toBCCEAddress = String.Empty;
    string subject = "Acknowledgement Mail - " + ProcessCode;
    
    string statusString = String.Empty;

    if (FailedStep == String.Empty) //No Failed Step before acknowledgment;
    {
        statusString = "The process got executed successfully and no errors/warnings were raised.";
    }
    else
    {
        statusString = "The process failed at the step : " + ProcessInformation.CurrentStepCode;
    }

    string bodyHtml = @"<html>
                                    <body>
                                    <p>Hi,</p>
                                    <p>hello nice to meet you: </p>
                                    
    
                                    <p>Regards,</p>
                                    <p>ABC Team</p>
                                    </body>
                                    </html>
                                    ";
    List<string> attachments = new List<string>();
    
    string LogPath = ConfigurationManager.AppSettings["Log"];
    string file = Path.Combine(LogPath, fileGuid.ToString());
    
    attachments.Add(file);

    PulseTest("Acknowledgement|SendFinalStatusMail| Prepare Mail Content completed");
    
    PulseTest("Acknowledgement|SendFinalStatusMail| Send Mail started");
    
    SendMail(toEmailAddresses, toCCEAddress, toBCCEAddress, subject, bodyHtml, attachments);
    
    PulseTest("Acknowledgement|SendFinalStatusMail| Send Mailcompleted");
}

First, change your method so that it gets an IEnumerable of strings, that represent the emails.首先,更改您的方法,使其获得一个IEnumerable字符串,代表电子邮件。 Next, try calling it by passing several possible combinations and check whether it correctly applies your parameters.接下来,尝试通过传递几种可能的组合来调用它,并检查它是否正确应用了您的参数。

Finally, you will need to decide how you will define what your dynamic email set is.最后,您需要决定如何定义动态 email 集是什么。 You could have it as a config setting if you are going to always send the emails to the same persons.如果您要始终将电子邮件发送给同一个人,则可以将其作为配置设置。 Or, you could store some data in a database and load the emails using a criteria that matches your need.或者,您可以将一些数据存储在数据库中,并使用符合您需要的标准加载电子邮件。

So, what you will definitely need is to refactor your code to something like this:因此,您肯定需要将代码重构为以下内容:

public void SendFinalStatusMail(Guid fileGuid, IEnumerable<String> emails) {
    /*Your code*/
}

and then you will need to figure out how you are going to define what to pass to this method.然后你需要弄清楚你将如何定义传递给这个方法的内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM