0

メールが添付されたメールを送信しようとしています。電子メールを送信でき、添付ファイルが添付されていますが、添付ファイルを開こうとすると、Outlook で次のメッセージが表示されます。これは、電子メールの添付ファイルのエンコーディングに関係していると思います

:ここに画像の説明を入力

Outlook からのエラー

MailMessage mm1 = new MailMessage();
mm1.IsBodyHtml = true;
mm1.Body = "Body for person to approve";
mm1.Subject = "Home Owner's Insurance Policy";
mm1.From = new MailAddress("insurance@email.com", "SAHL");
mm1.ReplyTo = new MailAddress("insurance@email.com");
mm1.To.Add("ross.kriel@email.co.za");


foreach (NewBusinessData item in lData)
{

    MailMessage mm = new MailMessage();
    mm.IsBodyHtml = true;
    mm.Body = HTMLBody;
    mm.Subject = "Home Owner's Insurance Policy";
    mm.From = new MailAddress("insurance@email.com", "SAHL");
    mm.ReplyTo = new MailAddress("insurance@email.com");

    byte[] thisAttachment;
    thisAttachment = Common.Attach(Settings.Default.NewBusinessCSFDataFileWriterPath +                item.PolicyNumber + "_" + item.MortgageLoanAccountNumber + ".pdf");

     Stream ClientPDF = new MemoryStream(thisAttachment);

     Attachment attStaticPDF = new Attachment(StaticPDF, "Home Owner's Insurance Policy.pdf");

     Attachment attClientPDF = new Attachment(ClientPDF, item.PolicyNumber + ".pdf");
     mm.Attachments.Add(attStaticPDF);
     mm.Attachments.Add(attClientPDF);

     Assembly assembly = typeof(SmtpClient).Assembly;
     Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
     MemoryStream stream = new MemoryStream();

     ConstructorInfo mailWriterContructor =    mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
     object mailWriter = mailWriterContructor.Invoke(new object[] { stream });
     MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
     sendMethod.Invoke(mm, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);

    stream.Position = 0;
    Attachment emailAtt = new Attachment(stream, "Home Owner's Insurance Policy.msg");

    mm1.Attachments.Add(emailAtt);
 }

SmtpClient smtp1 = new SmtpClient();
smtp1.Host = "IPHost";
smtp1.Port = 25;
smtp1.UseDefaultCredentials = true;
smtp1.EnableSsl = false;
try
{
    smtp1.Send(mm1);
}
catch (Exception exd)
{
    Console.WriteLine(exd.ToString());
}
4

2 に答える 2

0

そのため、結局のところ、問題を引き起こしたのは Transfer Encoding でした。それを8ビットとして指定し、アタッチメントにも適切なメディアタイプがあることを確認すると、問題は解決しました。

MailMessage mm1 = new MailMessage();
mm1.IsBodyHtml = true;
mm1.Body = ReportMsgBody;
mm1.Subject = "Home Owner's Insurance Policy Proofs: " + lData.Select(x => x.FileName).First();
mm1.From = new MailAddress("insurance@email.com", "FromName");
mm1.ReplyTo = new MailAddress("insurance@email.com");
mm1.To.Add(receiver.EmailAddress);

foreach (NewBusinessData item in lData)
{
    MailMessage mm = new MailMessage();
    mm.IsBodyHtml = true;
    mm.Body = HTMLBody;
    mm.Subject = "Home Owner's Insurance Policy";
    mm.From = new MailAddress("insurance@email.com", "FromName");
    mm.ReplyTo = new MailAddress("insurance@email.com");
    mm.To.Add(item.EmailAddress);

    byte[] thisAttachment;
    thisAttachment = Common.Attach(Settings.Default.FileWriterPath + item.PolicyNumber + "_" + item.MortgageLoanAccountNumber + ".pdf");
    Stream ClientPDF = new MemoryStream(thisAttachment);
    Attachment attClientPDF = new Attachment(ClientPDF, item.Pr + ".pdf", "application/pdf");
    mm.Attachments.Add(attClientPDF);

    byte[] thisAttachment2;
    thisAttachment2 = Common.Attach(Settings.Default.StaticAttatchmentPath + "Home Owner's Insurance Policy.pdf");
    Stream StaticPDF = new MemoryStream(thisAttachment2);
    Attachment attStaticPDF = new Attachment(StaticPDF, "Home Owner's Insurance Policy.pdf", "application/pdf");
    mm.Attachments.Add(attStaticPDF);

    Assembly assembly = typeof(SmtpClient).Assembly;
    Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
    MemoryStream stream = new MemoryStream();

    ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
    object mailWriter = mailWriterContructor.Invoke(new object[] { stream });
    MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
    sendMethod.Invoke(mm, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);

    stream.Seek(0, SeekOrigin.Begin);
    Attachment emailAtt = new Attachment(stream, "Home Owner's Insurance Policy", "message/rfc822");
    emailAtt.TransferEncoding = System.Net.Mime.TransferEncoding.EightBit;
    mm1.Attachments.Add(emailAtt);
}
于 2013-09-21T13:59:28.443 に答える
0

Outlook の msg 形式はバイナリ ファイル形式ですMailWriter。SMTP サーバーが (RFC 821 に従って) データを受信すると予想される方法でメールを書き込みます。つまり、プレーンテキスト形式です。そのため、添付しようとしているファイルは、Outlook が想定している形式ではありません。

于 2013-09-19T15:58:50.703 に答える