我正在使用system.net.mail库向用户发送有关某些动作的信息。在程序运行时,我正在收集一些信息,稍后我想将其显示给用户。邮件内容将如下所示:
Hello user,
this is your id in system.
*if he chosen option1*
You chosen option 1 value
*if he chosen option2*
You chosen option 2 value
*if he chosen option3*
You chosen option 3 value问题是我没有找到在程序运行时如何更改邮件内容(用html编写并添加到资源中)的方法。
任何人都可以建议我应该如何编辑邮件内容,这取决于选择的值,或者可能有任何替代方案?
邮件示例:
`<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">`
<html>
<head>
<META http-equiv=Content-Type content="text/html">
</head>
<body>
<p>Hello user,</p>
<p>This is your id in system : {0}</p>
/*
And the dificult part here:
if he chosen option1 in program I want in email to show that option 1 value.
And if he didn't chosen it I don't wanna show it to him.
*/
</body>
</html>发布于 2013-04-11 18:34:55
您可以将html存储在XML文件中,并通过string.Format填充内容,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<Email>
<FromAddress>from</FromAddress>
<ToAddress>to</ToAddress>
<Subject>subject line</Subject>
<EmailBody>
<![CDATA[
<html>
<head>
<title>Customer</title>
</head>
<div valign="top">
<font color="#666666" face="Arial, Helvetica, sans-serif, Verdana" size="2">
<p>Hello user.</p>
<p><strong>This is your ID in the system: </strong>{0}<br />
<strong>You chose option: </strong>{1}<br /></p>
</font>
</div>
</html>
]]>
</EmailBody>
</Email>代码(填充和发送):-
int custId = //provide customer id
string option = //customers selected option
string custEmail = //customers email
MailMessage mail = GetHtmlEmail();
string message = string.Format(mail.Body, custId, option);
mail.IsBodyHtml = true;
mail.Body = message;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}读取电子邮件标记+设置邮件对象的一些属性:-
private MailMessage GetHtmlEmail()
{
MailMessage mail = new MailMessage();
XmlTextReader xReader = new XmlTextReader(Server.MapPath("PATH TO EMAIL.XML"));
while (xReader.Read())
{
switch (xReader.Name)
{
case "ToAddress":
mail.To.Add(xReader.ReadElementContentAs(typeof(string), null).ToString());
break;
case "FromAddress":
mail.From = new MailAddress(xReader.ReadElementContentAs(typeof(string), null).ToString());
break;
case "Subject":
mail.Subject = xReader.ReadElementContentAs(typeof(string), null).ToString();
break;
case "EmailBody":
mail.Body = xReader.ReadElementContentAs(typeof(string), null).ToString();
break;
default:
break;
}
}
return mail;
}编辑*如果你根本不想显示这个<strong>You chose option: </strong>{1}<br />如果客户没有选择任何选项,那么你可以这样做(尽管有点老生常谈):-
if(!string.IsNullOrEmpty(option))
{
option = string.Format("<strong>You chose option: </strong>{1}<br />", option);
}
else
{
option = string.Empty;
}然后像正常一样传递它:
string message = string.Format(mail.Body, custId, option);确保将标记<strong>You chose option: </strong>{1}<br />中的此行替换为{1}
发布于 2013-04-11 19:03:52
我有一个现实世界的情况,我需要用电子邮件做这件事,我使用了模板。您可以尝试为电子邮件正文创建一个带有变量的模板,并根据您的情况创建此正文,将变量更改为正确的值。模板示例:
<p>Hello user [User]</p>
<p>This is your id in system : [ID]</p>
<p>[ChoosenOption]</p>创建一个资源文件,这样你就可以通过它的名字来获取这个模板。您必须创建一个更改这些变量的方法,如下例所示:
public String ProcessTemplate(String templateName, dynamic variables)
{
String template = MethodThatSearchOnResourceAndReturnsTemplateByItsName(templateName);
if (String.IsNullOrWhiteSpace(template))
{
return String.Empty;
}
if (variables == null)
{
return template;
}
PropertyInfo[] properties = ((Object)variables).GetType().GetProperties();
foreach (PropertyInfo prop in properties )
{
template = template.Replace("[" + prop.Name + "]", propriedade.GetValue(properties, null));
}
return template;
}现在您可以像这样绘制模板:
String body = ProcessTemplate("TemplateName", new
{
User = the user name,
ID = this user id,
ChoosenOption = ReturnChoosenOptionHtml()
});
private String ReturnChoosenOptionHtml()
{
String html = String.Empty;
if(chooseOption1)
html += "xptoHTML";
if(choosenOption2)
html += "abcdHTML";
return html;
}最终结果将是一封电子邮件,其中的变量随您传递的值进行了更改。
发布于 2013-04-11 20:17:12
当您从用户提交表单时,将他们的信息保存在会话中,并在发送邮件后显示弹出窗口,并将您的信息与会话连接起来。
https://stackoverflow.com/questions/15946358
复制相似问题