標題沒有明智的意義,但本質上我想使用會話變量並將它們包含在c#代碼中的電子郵件正文中。發送沒有會話變量的電子郵件可以正常工作。是否有可能做我想做的事?如何在電子郵件中使用會話變量
修改後的代碼:
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);
}
}
也要注意,Environment.NewLine
不起作用。 您的幫助將是非常讚賞
糾正了這個請告訴我問題呢? –
@ Smith.h.Neil問題是,我想使用的會話變量不會出現在電子郵件 – Lemons
我看起來像我正確地從會話中獲取值。你確定他們在得到他們後不是空的嗎? –