system.net.mail编辑邮件内容

时间:2013-04-11 10:22:43

标签: c# html asp.net-mvc email

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

3 个答案:

答案 0 :(得分: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}中的此行

答案 1 :(得分: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;
}

最终结果将是一封电子邮件,其中[变量]随您传递的值而变化。

答案 2 :(得分:1)

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

相关问题