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

string.Formathtml を XML ファイルに保存し、次のようにコンテンツを入力できます。

<?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-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;
}

最終的な結果は、[変数] が渡された値で変更されたメールになります。

于 2013-04-11T11:03:51.577 に答える
1

ユーザーからフォームを送信するときに、その情報をセッションに保存し、メールを送信した後、ポップアップを表示してセッションと情報を連結します。

于 2013-04-11T12:17:12.317 に答える