|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Error while sending e-mails with attachments using SmtpClient::SendAsyncSmtpClient::SendAsync Options There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic. There was an error processing your request. Please try again. Standard view View as tree Proportional text Fixed text 1 message - Collapse all The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet. Your reply message has not been sent. Your post was successful mukesh bhakta View profile More options May 14, 10:46 am Newsgroups: microsoft.public.dotnet.framework.windowsforms From: mukesh bhakta <mukesh_bha***@hotmail.com> Date: 13 May 2007 17:46:04 -0700 Local: Mon, May 14 2007 10:46 am Subject: Error while sending e-mails with attachments using SmtpClient::SendAsync Reply | Reply to author | Forward | Print | Individual message | Show original | Remove | Report this message | Find messages by this author 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 |
|||||||||||||||||||||||