Retreive Email from SQL server and load it into Aspose.Email.Mail

Hi,

We have a provider who is sending email (Binary) + XML file into our SQL server thru a Webservice.

We want to open this email into a web page and get access to attachement. The mail is a EML file store in SQL as a varbinary.

Could you please explain me how to retreive this binary into aspose.EMail.Mail object?

Regards

Guillaume

Hi Guillaume,


Thanks for writing to Aspose.Email support team.

There are many methods to read binary data (BLOB) from SQL server like here. Once the EML binary data is read into byte array, that can be used to create MailMessage.

In the following sample code it is assumed that binary data is read from SQL server and saved in byte array. To fulfill this assumption an EML file is loaded into byte array and rest of the code is same to load EML into MailMessage.

Please give a try to the following code and let us know your feedback.

// Here byteArray is filled from SQL Server or some other source
byte[] byteArray = System.IO.File.ReadAllBytes(“Sample.eml”);

// Create a memory stream object
MemoryStream memStr = new MemoryStream();

// Create BinaryWriter object
BinaryWriter bw = new BinaryWriter(memStr);
// Write byte array into MemoryStream
bw.Write(byteArray);
bw.Flush();
// Load EML from MemoryStream to MailMessage
MailMessage mail = MailMessage.Load(memStr);
// Save it as another EML and open to check the output email
mail.Save("Output.eml");

Hi,


Thank you for you answer. I found the solution as you describe it and write this:

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get(“SqlServer”)))
{
using (SqlCommand command = new SqlCommand(“select Doc from FaxTrack.Input where Filename = @FILENAME “, conn))
{
command.Parameters.Add(”@FILENAME”, SqlDbType.NVarChar).Value = “AVM-FAXBIS-2_1TExdf-00015d-VK.eml”;
conn.Open();
Stream stream = new MemoryStream((byte[])command.ExecuteScalar());
mymail.Import(stream);
L_From.Text = mymail.From.Address.ToString();
L_To.Text = mymail.To.ToString();
L_Subject.Text = mymail.Subject.ToString();
L_Body.Text = mymail.HtmlBody.ToString();
}
conn.Close();

}

Now I am trying to list all Attachement and be able to diplay them. Could you please help me by providing an example?

If I succeed it this morning, I will be able to order the component.

Regards
Guillaume

Hi,


I succeed to list all Attachment with this code:

foreach (Aspose.Email.Mail.Attachment Att in mymail.Attachments)
{
if (L_Attachement.Text == “”)
{
L_Attachement.Text = Att.Name.ToString();
}
else
{
L_Attachement.Text = L_Attachement.Text + "; \n " + Att.Name.ToString();
}
}

But now I want to use a page to open the Attachment by using Response.BinaryWrite

How Do I convert mymail.Attachments[filename]? How Can I convert an attachment to Byte[]?

Regards
Guillaume

Hi,


I succeed !!!

I create this function to convert Stream to byte[]
private byte[] ToByteArray(Stream inputStream)
{

using (MemoryStream ms = new MemoryStream())
{

inputStream.CopyTo(ms);

return ms.ToArray();

}

}

Regards
Guillaume

Hi Guillaume,

Thank you for using Aspose.Email and providing the feedback.

The Attachment class provides a function to save it either as stream or to the disc. For your requirements of converting an attachment to Byte[], please have a look at the following lines of code. This code saves the attachment to MemoryStream and converts the stream to byte[] then. Please give it a try and let us know your feedback.

//Get the attachment
Aspose.Email.Mail.Attachment att = main.Attachments[0];

MemoryStream stream = new MemoryStream();

//Save the attachment to Stream
att.Save(stream);

//Define a byte array to save the stream to it
long iTotalLength = stream.Length;
byte[] byteArr= new byte[iTotalLength];

//save the stream to byte array
byteArr = stream.ToArray();