Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My problem is, I have to send mail with session variable.
I have a textbox with session variable.
HTML
<input type="search" name="txtValue" id="txtValue" value="@Session["searched"]" placeholder="Need Service ? >

I need to pass session variable to Email manager

Code

C#
public class Alert
{
    private const string EmailFrom = "XXX@gmail.com";
    public static void SendConfirmationEmailAlert(string alert_text_box)
    {
        using (var client = new SmtpClient())
        {
            using (var message = new MailMessage(EmailFrom, alert_text_box))
            {
                message.Subject = "Please Verify Your Account";
                message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + alert_text_box +
                    ", </p><p>Thankyou for Alert </p>"
 
                    + "</a></p><div>Best regards,</div><div>Jajin</div><p>Do not forward "
                    + "this email. The verify link is private.</p></body></html>";
                message.IsBodyHtml = true;
                client.EnableSsl = true;
                client.Send(message);
            };
        };
    }
}


How can i pass @Session["searched"] to the body(message.Body) of mail. I mean i want to send mail with session variable. How can i solve this?
Posted
Updated 30-Aug-15 18:30pm
v2

1 solution

C#
//If the values are in the cookie, then you can just write the cookie value to a string and plug it in. The session itself cannot be sent as it will be encrypted, but you can send the value(s) that it contains as shown below.
string cookieValue = Session["searched"]).ToString();
message.Body = "Dear " + cookieValue + ". Thankyou for Alert<br /><br />Best regards, Jalin<br /><br />Do not forward this email. the verify link is private.";
 
Share this answer
 
Comments
Jajin Nelson 31-Aug-15 0:34am    
session not exists in current context error !!
jgakenhe 31-Aug-15 6:05am    
Try adding runat="server" to the HTML tag to make sure it is run in the code behind.

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