简体   繁体   English

发送带有MailMessage的MailMessage作为附件

[英]Send MailMessage With MailMessage as attachments

I am attempting to send an email that has emails as attachments. 我正在尝试发送包含电子邮件作为附件的电子邮件。 I am able to send an email and it comes through with the attachments but I get the following message in outlook when trying to open any of the attachments. 我能够发送电子邮件,并且附带附件,但是在尝试打开任何附件时,Outlook中都会收到以下消息。 I think this has to do with the encoding of the email attachment 我认为这与电子邮件附件的编码有关

: 在此处输入图片说明

来自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());
}

Outlook's msg format is a binary file format - the MailWriter writes the mail in a manner that an SMTP server would expect to recieve the data (per RFC 821) - ie a plaintext format. Outlook的msg格式是二进制文件格式MailWriter以SMTP服务器希望接收数据的方式(按照RFC 821)写入邮件-即纯文本格式。 As such, the file you're attaching isn't in the format that Outlook is expecting. 因此,您要附加的文件不是Outlook期望的格式。

So at the end of the day it was the Transfer Encoding that caused the problem. 因此,最终导致问题的是传输编码。 Specifying it as Eight bit and ensuring the attachment also had the right media type solved the problem. 将其指定为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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM