发送带有电子邮件附件的电子邮

时间:2013-09-19 13:24:24

标签: c# smtp .net-4.5

我正在尝试发送包含附件的电子邮件。 例外:“发送电子邮件失败” 内部异常:“无法访问封闭的流”

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", "ReplyName");
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","ReplyName");
    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");
    using (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);

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

         mm1.Attachments.Add(emailAtt);                                                    
     }                                                                                                                                                                    
 }
 SmtpClient smtp1 = new SmtpClient();
 smtp1.Host = "HostIP";
 smtp1.Port = 25;
 try
 {
     smtp1.Send(mm1);
 }
 catch (Exception exd)
 {
     Console.WriteLine(exd.ToString());
 }

4 个答案:

答案 0 :(得分:1)

简单地将使用条款包裹在整个事物中。一旦执行了batch-code inside using子句,您的流就会超出范围。

试试这个:

using (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);

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

         mm1.Attachments.Add(emailAtt);                                                                                                                                                                                                                       
 SmtpClient smtp1 = new SmtpClient();
 smtp1.Host = "HostIP";
 smtp1.Port = 25;
 try
     smtp1.Send(mm1);
} // Ending using Clause

或者实际上不要在这里使用Using子句,因为这里不需要它。

答案 1 :(得分:1)

所以在一天结束时,传输编码导致了问题。将其指定为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);
}

答案 2 :(得分:0)

一旦退出“使用”范围,Stream就会关闭。相反,在发送

后不要使用但关闭流
 try
 {
   smtp1.Send(mm1);
   stream.Close();
 }
 catch (Exception exd)
 {
   Console.WriteLine(exd.ToString());
 }

答案 3 :(得分:0)

由于使用,你正在丢失MemoryStream。你不必在这里使用。

using (MemoryStream stream = new MemoryStream())
{

使用:

MemoryStream stream = new MemoryStream()

不需要使用,因为没有绑定到memoryStream的非托管句柄(如FileStream)

如果您仍想使用using / dispose方法。您可以将它们添加到列表中并在最后进行清理。