[英]system.net.mail editing mail content
我正在使用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>
您可以将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 />
替换此行<strong>You chose option: </strong>{1}<br />
用{1}
我有一个现实世界的情况,我需要使用电子邮件来完成此任务,并且我使用了模板。 您可以尝试使用变量为电子邮件正文创建一个模板,然后根据您的情况创建该正文,将变量更改为正确的值。 模板示例:
<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;
}
最终结果将是一封电子邮件,其中[Variables]随您传递的值而更改。
当您从用户提交表单时,将其信息保存在会话中,然后在发送邮件后显示一个弹出窗口,并将您的信息与会话连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.