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
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