简体   繁体   中英

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. I want to send the email to the user who has logged in and using the application and to admin. how can I change the hardcoded email address to a dynamic one

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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