Error while sending e-mails with attachments using SmtpClient::SendAsync

M

mukesh bhakta

Hi guys,

I get an error when I send e-mails with attachments using SmtpClient.
Following is the code snippet -

MailMessage msg = new MailMessage(from, to);
/*...all the initialization goes here */

byte [] data = /* some binary data */
using (MemoryStream ms = new MemoryStream(/*att.Contents*/))
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(data);
sw.Flush();
ms.Position = 0;

System.Net.Mail.Attachment natt = new
System.Net.Mail.Attachment(
ms, "test.txt");
msg.Attachments.Add(natt);
}
}

private static void sendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
logmsg("E-mail send cancelled");
}
else if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
logmsg(e.Error.Message + ' ' +
e.Error.InnerException.Message);
}
else
{
logmsg(success)
}
}

The error I am getting in the above method is -

System.Net.Mail.SmtpException: Failure sending mail. --->
System.NotSupportedException: Stream does not support reading.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult
result)
--- End of inner exception stack trace ---

What am I doing wrong? The code seems to work fine if no attachment is
supplied.

cheers

MB
 
B

Bryan Phillips

You don't need to use StreamWriter to populate a MemoryStream.

Just pass your byte array into the MemoryStream's constructor: using
(MemoryStream ms = new MemoryStream(data))

Leave the line ms.Position = 0 though.
 
M

mukesh bhakta

You don't need to use StreamWriter to populate a MemoryStream.

Just pass your byte array into the MemoryStream's constructor: using
(MemoryStream ms = new MemoryStream(data))

Leave the line ms.Position = 0 though.

--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net




I get an error when I send e-mails with attachments using SmtpClient.
Following is the code snippet -
MailMessage msg = new MailMessage(from, to);
/*...all the initialization goes here */
byte [] data = /* some binary data */
using (MemoryStream ms = new MemoryStream(/*att.Contents*/))
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(data);
sw.Flush();
ms.Position = 0;
System.Net.Mail.Attachment natt = new
System.Net.Mail.Attachment(
ms, "test.txt");
msg.Attachments.Add(natt);
}
}
private static void sendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
logmsg("E-mail send cancelled");
}
else if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
logmsg(e.Error.Message + ' ' +
e.Error.InnerException.Message);
}
else
{
logmsg(success)
}
}
The error I am getting in the above method is -
System.Net.Mail.SmtpException: Failure sending mail. --->
System.NotSupportedException: Stream does not support reading.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult
result)
--- End of inner exception stack trace ---
What am I doing wrong? The code seems to work fine if no attachment is
supplied.

MB- Hide quoted text -

- Show quoted text -

Bryan,

That doesnt work.

I am not sure if you have tried this out yourself but if you look at
my code snippet you will notice this is the first thing I tried and
later moved it to the Write().

Cheers

MB
 
M

Moty Michaely

You don't need to use StreamWriter to populate a MemoryStream.
Just pass your byte array into the MemoryStream's constructor: using
(MemoryStream ms = new MemoryStream(data))
Leave the line ms.Position = 0 though.
--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net
Hi guys,
I get an error when I send e-mails with attachments using SmtpClient.
Following is the code snippet -
MailMessage msg = new MailMessage(from, to);
/*...all the initialization goes here */
byte [] data = /* some binary data */
using (MemoryStream ms = new MemoryStream(/*att.Contents*/))
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(data);
sw.Flush();
ms.Position = 0;
System.Net.Mail.Attachment natt = new
System.Net.Mail.Attachment(
ms, "test.txt");
msg.Attachments.Add(natt);
}
}
private static void sendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
logmsg("E-mail send cancelled");
}
else if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
logmsg(e.Error.Message + ' ' +
e.Error.InnerException.Message);
}
else
{
logmsg(success)
}
}
The error I am getting in the above method is -
System.Net.Mail.SmtpException: Failure sending mail. --->
System.NotSupportedException: Stream does not support reading.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult
result)
--- End of inner exception stack trace ---
What am I doing wrong? The code seems to work fine if no attachment is
supplied.
cheers
MB- Hide quoted text -
- Show quoted text -

Bryan,

That doesnt work.

I am not sure if you have tried this out yourself but if you look at
my code snippet you will notice this is the first thing I tried and
later moved it to the Write().

Cheers

MB

Hi,

Is it working with synchronous send?

Moty
 
M

mukesh bhakta

You don't need to use StreamWriter to populate a MemoryStream.
Just pass your byte array into the MemoryStream's constructor: using
(MemoryStream ms = new MemoryStream(data))
Leave the line ms.Position = 0 though.
--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net

Hi guys,
I get an error when I send e-mails with attachments using SmtpClient.
Following is the code snippet -
MailMessage msg = new MailMessage(from, to);
/*...all the initialization goes here */
byte [] data = /* some binary data */
using (MemoryStream ms = new MemoryStream(/*att.Contents*/))
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(data);
sw.Flush();
ms.Position = 0;
System.Net.Mail.Attachment natt = new
System.Net.Mail.Attachment(
ms, "test.txt");
msg.Attachments.Add(natt);
}
}
private static void sendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
logmsg("E-mail send cancelled");
}
else if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
logmsg(e.Error.Message + ' ' +
e.Error.InnerException.Message);
}
else
{
logmsg(success)
}
}
The error I am getting in the above method is -
System.Net.Mail.SmtpException: Failure sending mail. --->
System.NotSupportedException: Stream does not support reading.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult
result)
--- End of inner exception stack trace ---
What am I doing wrong? The code seems to work fine if no attachment is
supplied.
cheers
MB- Hide quoted text -
- Show quoted text -

That doesnt work.
I am not sure if you have tried this out yourself but if you look at
my code snippet you will notice this is the first thing I tried and
later moved it to the Write().

MB

Hi,

Is it working with synchronous send?

Moty- Hide quoted text -

- Show quoted text -

Nope. Send() is even worst. It doesn't come back with any errors.
 
M

Moty Michaely

On May 14, 12:05 pm, "Bryan Phillips"
You don't need to use StreamWriter to populate a MemoryStream.
Just pass your byte array into the MemoryStream's constructor: using
(MemoryStream ms = new MemoryStream(data))
Leave the line ms.Position = 0 though.
--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net

Hi guys,
I get an error when I send e-mails with attachments using SmtpClient.
Following is the code snippet -
MailMessage msg = new MailMessage(from, to);
/*...all the initialization goes here */
byte [] data = /* some binary data */
using (MemoryStream ms = new MemoryStream(/*att.Contents*/))
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(data);
sw.Flush();
ms.Position = 0;
System.Net.Mail.Attachment natt = new
System.Net.Mail.Attachment(
ms, "test.txt");
msg.Attachments.Add(natt);
}
}
private static void sendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
logmsg("E-mail send cancelled");
}
else if (e.Error != null)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
logmsg(e.Error.Message + ' ' +
e.Error.InnerException.Message);
}
else
{
logmsg(success)
}
}
The error I am getting in the above method is -
System.Net.Mail.SmtpException: Failure sending mail. --->
System.NotSupportedException: Stream does not support reading.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult
result)
--- End of inner exception stack trace ---
What am I doing wrong? The code seems to work fine if no attachment is
supplied.
cheers
MB- Hide quoted text -
- Show quoted text -
Bryan,
That doesnt work.
I am not sure if you have tried this out yourself but if you look at
my code snippet you will notice this is the first thing I tried and
later moved it to the Write().
Cheers
MB

Is it working with synchronous send?
Moty- Hide quoted text -
- Show quoted text -

Nope. Send() is even worst. It doesn't come back with any errors.

It stuck?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top