0

我正在使用 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>
4

3 回答 3

1

您可以将 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 />在客户没有选择任何选项的情况下出现,那么您可以这样做(虽然有点hacky):-

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-11T10:34:55.313 回答
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] 使用您传递的值进行了更改。

于 2013-04-11T11:03:51.577 回答
1

当您从用户提交表单时,将他们的信息保存在会话中,并在发送邮件后,显示一个弹出窗口并将您的信息与会话连接。

于 2013-04-11T12:17:12.317 回答