2012-02-07 5 views
1

私は、ユーザーに送信される電子メールにHTML形式のログシートを添付する必要があるというプロジェクト要件があります。 私はログシートを身体の一部にしたくありません。ログシートが非常に複雑なので、HTMLTextWriterまたはStringBuilderを使用しないでください。実行時にHTMLファイルを生成し、電子メールの添付ファイルとして送信

私が言及していない別の方法やこれを簡単にするツールはありますか?

注:私はMailDefinitionクラスを使って作業し、テンプレートを作成しましたが、それが可能であれば、これを添付ファイルにする方法が見つかりませんでした。

答えて

3

WebFormsを使用しているので、私はrendering your log sheet in a Control as a string、次にattaching that to a MailMessageをお勧めします。

レンダリング部分は少し次のようになります。

public static string GetRenderedHtml(this Control control) 
{ 
    StringBuilder sbHtml = new StringBuilder(); 
    using (StringWriter stringWriter = new StringWriter(sbHtml)) 
    using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter)) 
    { 
     control.RenderControl(textWriter); 
    } 
    return sbHtml.ToString(); 
} 

編集可能なコントロール(TextBoxDropDownList、など)を持っている場合、あなたはGetRenderedHtml()を呼び出す前に、ラベルやリテラルでそれらを交換する必要があります。完全な例については、this blog postを参照してください。ここで

MSDN example for attachmentsです:

// Specify the file to be attached and sent. 
// This example assumes that a file named Data.xls exists in the 
// current working directory. 
string file = "data.xls"; 
// Create a message and set up the recipients. 
MailMessage message = new MailMessage(
    "[email protected]", 
    "[email protected]", 
    "Quarterly data report.", 
    "See the attached spreadsheet."); 

// Create the file attachment for this e-mail message. 
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 
// Add time stamp information for the file. 
ContentDisposition disposition = data.ContentDisposition; 
disposition.CreationDate = System.IO.File.GetCreationTime(file); 
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 
// Add the file attachment to this e-mail message. 
message.Attachments.Add(data); 
2

あなたは電子メールテンプレート用のカミソリを使用することができます。 RazorEngineまたはMvcMailerは、私は現在、私は、これはオプションであるとは考えていないMVCを使用していないあなた

Use Razor views as email templates inside a Web Forms app

Razor views as email templates

http://www.codeproject.com/Articles/145629/Announcing-MvcMailer-Send-Emails-Using-ASP-NET-MVC

http://kazimanzurrashid.com/posts/use-razor-for-email-template-outside-asp-dot-net-mvc

+0

のための仕事をするかもしれません。 – Matt

+0

@Matt razorテンプレートはウェブフォームでも機能します。 – adt

+2

これは知っておきたいことですが、残念ながらasp.net 3.5をまだ実行していて、RazorEngineには4.0が必要です – Matt

関連する問題