Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call a method(contains email text Body) from two different methods whose logical requirement is same but each method has different textual email body. How can I do that.
In the below code, when it is called by two different methods the only difference is in the email body

What I have tried:

This is the method that i want to call:
C#
private bool GenerateEmailOTP(int randomNumber, string emailId)
        {
            var MailUrl = ConfigurationManager.AppSettings["MailUrl"];
            var senderEmail = ConfigurationManager.AppSettings["SenderEmail"];

            var subject = "Verification Code from Broadspire SSP";

            var emailBody = @"Dear Claimant,
                                <br>
                                <br>
                                Thank you for contacting Broadspire. 
                                <br>
                                <br>
                                Please enter following code {0} to generate New Pin. 
                                <br>
                                <br>
                                Thank you,
                                <br>
                                Broadspire SSP Team.";
            var body = String.Format(emailBody, randomNumber);

            Email oEmail = new Email(Config.MailSettings_Smtp_From, emailId, subject, body);
            oEmail.Message.IsBodyHtml = true;
            oEmail.Send(false);

            return true;
        }
Posted
Updated 20-Feb-18 23:40pm
v2
Comments
Dylvh 21-Feb-18 5:21am    
Do you want the email body to be different when calling the method from two other different methods? Then, your method returns a Boolean but it will always return true. Don't you want to rather change from bool to void? Or is there a reason for it being bool?
Avinash Gupta 21-Feb-18 5:25am    
Its an webApi so it has to be used for front end purpose
BillWoodruff 21-Feb-18 10:32am    
Clarify this: "each method has different textual email body." Other than the content of a string, what other difference is there ?
RickZeeland 22-Feb-18 11:51am    
If you downvoted my solution, could you tell me why ?

You could use an optional parameter, e.g.
private bool GenerateEmailOTP(int randomNumber, string emailId, bool body2 = false)
See: [Dotnetperls]
 
Share this answer
 
I'd store the different email bodies in the config file (probably web.config) and add a parameter to the GenerateEmailOTP method that lets the calling code specify which email body they want, and the method will retrieve the appropriate mail body based on what that parameter is.

private bool GenerateEmailOTP(int randomNumber, string emailId, string emailType)


The simplest way of doing this would be to store the different configs using a settings name like "newPinBody" and "someOtherBody" and the calling code will pass "newPin" or "someOther" as a parameter to the function, and the code will then use ConfigurationManager to read the appropriate body

string body = ConfigurationManager.AppSettings[emailType + "Body"]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900