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);
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)));
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
Hi-
Have you tried using 0.5.7b yet? I modified this for the last release.
Thanks,
-Mike
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)));
}