1

クライアントに一括メールを送信するシステムを作成しており、サイトごとにメールの HTML およびテキスト バージョンを保存して、ユーザーのメール クライアントが HTML をサポートしていない場合でも、テキスト ビューが適切にフォーマットされ、必要に応じてフォーマットされるようにしています。

HTML バージョンからプレーン テキスト バージョンを生成したくはありません。多くのメニュー リンクや、必要に応じて書式設定されていないその他のテキストが追加されるからです。

HTML 文字列を Body として追加し、テキスト文字列を AltBody として追加するため、これは Persits Email Component = http://www.aspemail.com/を使用する ASP クラシックで正常に機能します。

ただし、これを C# .NET 4.5 で複製するのに問題があります。

できるだけ多くの例に従いましたが、画像/バナーを探して HTML を渡す必要がある MailMessage オブジェクトを返し、URL を ContentID と LinkedResources に置き換える必要があるメソッドは、HTML とシンプルな HTML ビューで見栄えのする電子メールを何らかの形で返します(サンダーバードで)。

ただし、私が何をしても、プレーンテキストビューは常に、事前にフォーマットして使用したいテキスト文字列ではなく、オブジェクトがテキストに変換しようとしている HTML のバージョンのようです。

コードをデバッグすると、別のビューに追加する前に文字列が正しいことがわかるので、他に何をする必要があるかわかりません。

HTML を解析し、リンクされたリソースを追加し、MailMessage オブジェクトを返すメソッドには、次のコードがあります。

<pre>
/* I pass in a custom SiteEmail object with 2 properties HTMLEmail and TextEmail that hold both versions of the email */
public MailMessage ParseEmailImages(SiteEmail siteEmail)
{
    MailMessage email = new MailMessage();

    // extract the HTML version as we need to parse it to swap URLs for ContentID/Resources and paths etc
    string HTML = siteEmail.HTMLEmail;

    // this is a generic list to hold all my picture resource objects that I find (swapping image URLs to paths and contentIDs)
    List<LinkedResource> pictureResources = new List<LinkedResource>(); 


    // code to find all the paths, create image resource objects and add them to my list - and modify the HTML to reference
    // the new ContentIDs I swap the URLs for so the images are embedded in the email
    // ..... code .....

    // finished finding resource objects build email parts and return to caller

    // Add each alternate view to the message.

    // add the HTML view using my newly parsed HTML string
    ContentType HtmlContentType = new ContentType("text/html; charset=UTF-8");
    AlternateView altViewHTML = AlternateView.CreateAlternateViewFromString(HTML, HtmlContentType);
    altViewHTML.TransferEncoding = TransferEncoding.QuotedPrintable;

    // when I check here the siteEmail.TextEmail holds the CORRECT textual string I want BUT it's not displaying in the sent email

    ContentType PlainContentType = new ContentType("text/plain; charset=UTF-8");

    // as we didn't need to change anything in the text view we can just reference it straight out my custom email object siteEmail
    AlternateView altViewText = AlternateView.CreateAlternateViewFromString(siteEmail.TextEmail, PlainContentType);
    altViewText.TransferEncoding = TransferEncoding.QuotedPrintable;

    // loop through all my picture resource objects and ensure they are embedded into the HTML email
    foreach (LinkedResource pictureResource in pictureResources)
    {
        pictureResource.TransferEncoding = TransferEncoding.Base64;
        altViewHTML.LinkedResources.Add(pictureResource);
    }



    // add both parts of the email (text/html) which are both alternative views to message
    email.AlternateViews.Add(altViewText);
    email.AlternateViews.Add(altViewHTML);



    // return email object
    return email;
}


// a very cut down example of the calling method
public bool SendEmail()
{
    // parse our email object
    MailMessage EmailMailMessage = this.ParseEmailImages(this.SiteEmail);

    // send email
    EmailMailMessage.From = new MailAddress(this.SendFromEmail, this.SendFromName);

    EmailMailMessage.Subject = this.SendSubject;

    // ensure encoding is correct for Arabic/Japanese sites and body transfer method is correct
    EmailMailMessage.BodyEncoding = Encoding.UTF8;
        EmailMailMessage.BodyTransferEncoding = TransferEncoding.QuotedPrintable;

        SmtpClient client = new SmtpClient();

    // this in a try catch and more complex
    client.Send(this.EmailMailMessage);

}
</pre>

エンコード形式をいじってみたり、別のビューを 1 つ追加したりしましたが、古い ASP Classic コードと同じメールを送信できないようです。 WANT TO USE と HTML バージョンの 1 つ。私が起こりたくないHTMLバージョンから独自のプレーンテキストバージョンを常に作成しているようです。

どんな助けやアイデアも大歓迎です。

助けてくれてありがとう!

4

1 に答える 1