Menu

0.5.6b - FileAttachment as byte array (fix)

MrBool
2006-01-20
2013-04-02
  • MrBool

    MrBool - 2006-01-20

    I have tried to send PDF file (as byte array) as FileAttachment, but it was corrupted.

    below is a (dirty) fix, but it works for me ;)

    AbstractEmailAttachment.cs
    -> GetEncodedContents(IEncoder encoder)

    else if (_contentbytes!=null)
    {
    ((Base64Encoder)encoder).Encode(_contentbytes, new StringWriter(sb), charset);
    }

      /// <summary>
      /// Encode the File's data in base64.
      /// </summary>
      /// <param name="filestream">Reader for incoming string</param>
      /// <param name="stringwriter">Writer for outgoing encoded string</param>
      /// <param name="charset">The character set for the encoded string</param>
      public void Encode(FileStream filestream, StringWriter stringwriter, System.Text.Encoding charset)
      {
       byte[] buffer = new byte[filestream.Length];
       filestream.Read(buffer, 0, buffer.Length);
       filestream.Close();
       Encode(buffer, stringwriter,charset);
      }
      /// <summary>
      /// Encode bytes in base64.
      /// </summary>
      /// <param name="buffer">buffer of bytes</param>
      /// <param name="stringwriter">Writer for outgoing encoded string</param>
      /// <param name="charset">The character set for the encoded string</param>
      public void Encode(byte[] buffer, StringWriter stringwriter, System.Text.Encoding charset)
      {
       stringwriter.Write(MakeLines(System.Convert.ToBase64String(buffer, 0, buffer.Length)));
      }

    Test :

    using System;
    using System.IO;
    using DotNetOpenMail;
    using DotNetOpenMail.Utils;
    using NUnit.Framework;
    using log4net;
    namespace DotNetOpenMailTests
    {
    [TestFixture]
    public class QuickTest
    {

      SmtpServer _smtpserver=null;
      private static readonly ILog log = LogManager.GetLogger(typeof(QuickTest));
      public QuickTest()
      {
      }
      [SetUp]
      public void SetUp()
      {
      
      }
      [TearDown]
      public void TearDown()
      {
      }
      [Test]
      public void TestSend()
      {
       string email = "anonynmous@anonynmous.org";
       _smtpserver = new SmtpServer("the-exch-backend");
       EmailMessage emailmessage=new EmailMessage();
       emailmessage.FromAddress=new EmailAddress(email, "NUnit Test Sender");
       emailmessage.AddToAddress(new EmailAddress(email, "NUnit Test Sender"));
       emailmessage.Subject="EmailMessageTests Test HTML and Text";
       emailmessage.TextPart=new TextAttachment("This\r\nis the\r\ntext\r\npart.");
      
       FileInfo fileinfo = new FileInfo(@"C:\test.pdf");
       FileStream fs = fileinfo.OpenRead();
       byte[] bytes = new byte[fileinfo.Length];
       fs.Read(bytes,0,bytes.Length);
       fs.Close();
      
       //FileAttachment fileAttachment = new FileAttachment(fileinfo);
       FileAttachment fileAttachment = new FileAttachment(bytes);

       fileAttachment.FileName="test.pdf";
       fileAttachment.ContentType = "application/pdf";
      
       emailmessage.AddMixedAttachment(fileAttachment);
       emailmessage.Send(_smtpserver);
      }
       
    public static void Main()
    {
      new QuickTest().TestSend();
    }

    }
    }

    P.S: There is original message from me : http://www.gotdotnet.ru/Forums/Common/244794.aspx#245093

     
    • Mike Bridge

      Mike Bridge - 2006-01-20

      Hi-

      Have you tried using 0.5.7b yet?  I modified this for the last release.

      Thanks,

      -Mike

       
    • Serge V. Zabegailo

      0.5.7b - does not work.
      The solution from MrBool works perfectly.
      Just need to replace your code in AbstractEmailAttachment.cs :

      encoder.Encode(new BinaryReader(new MemoryStream(_contentbytes)), new StringWriter(sb));

      To:

      ((Base64Encoder)encoder).Encode(_contentbytes, new StringWriter(sb), charset);

      and add new routine in BaseEncoder64.cs
      public void Encode(byte[] buffer, StringWriter stringwriter, System.Text.Encoding charset) 
      {
      stringwriter.Write(MakeLines(System.Convert.ToBase64String(buffer, 0, buffer.Length)));
      }  

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.