簡體   English   中英

system.net.mail編輯郵件內容

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM