Question

The Title does not make brilliant sense but essentially I want to use session variables and include them within an email body within the c# code. Sending the email without session variables works fine. Is it even possible to do what I want?

REVISED CODE:

public partial class Success : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUser userName = Membership.GetUser(User.Identity.Name);

        ListBox order = (ListBox)(Session["order"]);

        string name = (String)(Session["name"]);
        string addressLine1 = (String)(Session["addressLine1"]);
        string addressLine2 = (String)(Session["addressLine2"]);
        string addressLine3 = (String)(Session["addressLine3"]);
        string county = (String)(Session["county"]);
        string postCode = (String)(Session["postcode"]);

        SmtpClient SmtpServer = new SmtpClient("smtp.live.com");

        var mail = new MailMessage();
        mail.From = new MailAddress("my email");
        mail.To.Add("recipient email");
        mail.Subject = "Your Reciept";
        mail.IsBodyHtml = true;

        string htmlBody;
        htmlBody = "Hi "+ userName + Environment.NewLine + Environment.NewLine +
                    "Here's your details: " + Environment.NewLine + Environment.NewLine
                    + order + Environment.NewLine + Environment.NewLine
                    + name + Environment.NewLine
                    + addressLine1 + Environment.NewLine
                    + addressLine2 + Environment.NewLine
                    + addressLine3 + Environment.NewLine
                    + county + Environment.NewLine
                    + postCode + Environment.NewLine + Environment.NewLine;

        mail.Body = htmlBody;
        SmtpServer.Port = 587;
        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Credentials = new System.Net.NetworkCredential("my email", "password");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }
}

also to note, Environment.NewLine does not work. Your help would be most appreciated

Was it helpful?

Solution 2

for those who helped me, sorry for wasting your time. I figured it out... really stupid but I was setting the session variables incorrectly in a previous webform before therefore the session variables were null.

the reason that there was no error was (my guess), I was setting the session variables on button_click function yet also having a postbackurl. the postbackurl must have overridden the button_click function.

So, For future reference. Originally I had

string name = Session["name"].ToString();
lblName.Text = name; //this didnt work

I corrected this by

Session["name"] = lblName.Text;

OTHER TIPS

It's not clear from your question where you are trying to access Session, but it is available from any code running in the web worker process (see my answer here). However, I prefer to put email logic in a separate class or even separate project.

The class can then take the input values as strings, or a type that represents your data.

// good
public void SendMail( string name, string address, etc etc )
{
   // mail code here
}

// better
public void SendMail( UserInfo info )
{

}

// best
public void SendMail( UserInfo info, string template )
{
   // insert the user values into a template of some sort...no hardcoding format/HTML
}

Regarding Environment.NewLine. Your string name is htmlBody, which implies you are sending an HTML email. You should use the appropriate markup instead of a newline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top