【发布时间】:2014-04-18 02:46:52
【问题描述】:
标题意义不大,但本质上我想使用会话变量并将它们包含在 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 问题是我想使用的会话变量没有出现在电子邮件中
-
在我看来,您从会话中正确获取了值。你确定他们拿到后不为空吗?
-
不要使用
Environment.NewLine,而是使用<br />(HTML 换行符),因为您正在发送 HTML 电子邮件而不是纯文本。 -
@Smith.h.Neil 很抱歉浪费了您的时间,但我想出了一个解决方案(请参阅我的答案)。你说得对,因为我从一开始就没有设置它们,所以在得到它们之后它们是空的。无论如何感谢您的宝贵时间
标签: c# asp.net email session session-variables