* .emlファイルや* .msgファイルとしてディスクにはMailMessageオブジェクトを保存する方法

StackOverflow https://stackoverflow.com/questions/1264672

質問

どのようにして、ディスクにはMailMessageオブジェクトを保存しますか? MailMessageオブジェクトは、任意の保存()メソッドを公開しません。

私はそれが* .emlファイルや* .msgの、いずれかの形式で保存した場合、問題を持っていけません。これを行うにはどのように任意のアイデア?

役に立ちましたか?

解決

は簡単にするために、私はちょうど接続項目から説明を引用しますのます:

  

あなたが実際に構成することができます   ファイルに電子メールを送信するSmtpClient   代わりに、ネットワークのシステム。あなたはできる   これは、プログラムで使用して行います   次のコード:

SmtpClient client = new SmtpClient("mysmtphost");
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\somedirectory";
client.Send(message);
     

また、あなたにこれを設定することができます   以下のようなアプリケーション構成ファイル   この:

 <configuration>
     <system.net>
         <mailSettings>
             <smtp deliveryMethod="SpecifiedPickupDirectory">
                 <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
             </smtp>
         </mailSettings>
     </system.net>
 </configuration>
  

、あなたがすべき電子メールを送信した後、   電子メールファイルが追加見ます   あなたが指定したディレクトリ。その後、することができます   別のプロセスを持って送り出します   バッチモードでの電子メールメッセージます。

あなたはそれがとにかくそれを送信されませんよう、代わりに列挙された1の空のコンストラクタを使用することができる必要があります。

他のヒント

ここでEMLデータを含むストリームにはMailMessageを変換する拡張メソッドです。 それは、ファイルシステムを使用しますが、それが動作するようハッキングの、その明らかに少します。

public static void SaveMailMessage(this MailMessage msg, string filePath)
{
    using (var fs = new FileStream(filePath, FileMode.Create))
    {
        msg.ToEMLStream(fs);
    }
}

/// <summary>
/// Converts a MailMessage to an EML file stream.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static void ToEMLStream(this MailMessage msg, Stream str)
{
    using (var client = new SmtpClient())
    {
        var id = Guid.NewGuid();

        var tempFolder = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);

        tempFolder = Path.Combine(tempFolder, "MailMessageToEMLTemp");

        // create a temp folder to hold just this .eml file so that we can find it easily.
        tempFolder = Path.Combine(tempFolder, id.ToString());

        if (!Directory.Exists(tempFolder))
        {
            Directory.CreateDirectory(tempFolder);
        }

        client.UseDefaultCredentials = true;
        client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        client.PickupDirectoryLocation = tempFolder;
        client.Send(msg);

        // tempFolder should contain 1 eml file

        var filePath = Directory.GetFiles(tempFolder).Single();

        // stream out the contents
        using (var fs = new FileStream(filePath, FileMode.Open))
        {
            fs.CopyTo(str);
        }

        if (Directory.Exists(tempFolder))
        {
            Directory.Delete(tempFolder, true);
        }
    }
}

あなたは、その後のthatsが返されたストリームを取り、あなたはディスク上の別の場所に保存したり、データベースのフィールドに格納し、あるいは添付ファイルとして電子メールで送信するなど、それを望むよう行うことができます。

は、をMailkit のを使用している場合。ただ、コードの下に記述します。

string fileName = "your filename full path";
MimeKit.MimeMessage message = CreateMyMessage ();
message.WriteTo(fileName);

一つの理由または別のためclient.send(右、そのメソッドを使用して実際の送信後に)失敗したので、私は良い「のOLE CDOとADODBストリームにプラグイン。私はまた、.Message値を設定する前に、template.emlでCDO.messageをロードする必要がありました。しかし、それは動作します。

上記一方がCがここでVBのための1つの

であるので
    MyMessage.From = New Net.Mail.MailAddress(mEmailAddress)
    MyMessage.To.Add(mToAddress)
    MyMessage.Subject = mSubject
    MyMessage.Body = mBody

    Smtp.Host = "------"
    Smtp.Port = "2525"
    Smtp.Credentials = New NetworkCredential(------)

    Smtp.Send(MyMessage)        ' Actual Send

    Dim oldCDO As CDO.Message
    oldCDO = MyLoadEmlFromFile("template.eml")  ' just put from, to, subject blank. leave first line blank
    oldCDO.To = mToAddress
    oldCDO.From = mEmailAddress
    oldCDO.Subject = mSubject
    oldCDO.TextBody = mBody
    oldCDO.HTMLBody = mBody
    oldCDO.GetStream.Flush()
    oldCDO.GetStream.SaveToFile(yourPath)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top