Home All Groups Group Topic Archive Search About

creating email attachement from stream, always empty.

Author
4 Jan 2007 12:23 PM
James
Hi, I have been trying to create a file attachement straight from a stream
without touching the disk, however, the attachement always turns out to be
empty, please help!! I tried attach a existing file and it worked, however I
really want to create the attachment on the fly. Here is my code, thanks!!!

            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
            smtp.Port =
Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
            {
                NetworkCredential cre = new
NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
                smtp.Credentials = cre;
            }
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;
            msg.To.Add(to);
            msg.From = new MailAddress(from);
            msg.Subject = subject;
            msg.Body = body;

            ContentType ct = new ContentType();
            ct.MediaType = MediaTypeNames.Text.Plain;
            ct.Name = "InspectionReport.htm";

            MemoryStream stream = new MemoryStream();

            StreamWriter writer = new StreamWriter(stream);
            writer.Write(body);

            Attachment attachement = new Attachment(stream, ct);

            msg.Attachments.Add(attachement);

            smtp.Send(msg);
            writer.Close();
            stream.Close();

Author
4 Jan 2007 2:07 PM
Henning Krause [MVP - Exchange]
Hello,

do a

    writer.Flush()

right before calling the Send method..

Best regards,
Henning Krause

Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> Hi, I have been trying to create a file attachement straight from a stream
> without touching the disk, however, the attachement always turns out to be
> empty, please help!! I tried attach a existing file and it worked, however
> I
> really want to create the attachment on the fly. Here is my code,
> thanks!!!
>
>            SmtpClient smtp = new SmtpClient();
>            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
>            smtp.Port =
> Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
>            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
>            {
>                NetworkCredential cre = new
> NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
>                smtp.Credentials = cre;
>            }
>            MailMessage msg = new MailMessage();
>            msg.IsBodyHtml = true;
>            msg.To.Add(to);
>            msg.From = new MailAddress(from);
>            msg.Subject = subject;
>            msg.Body = body;
>
>            ContentType ct = new ContentType();
>            ct.MediaType = MediaTypeNames.Text.Plain;
>            ct.Name = "InspectionReport.htm";
>
>            MemoryStream stream = new MemoryStream();
>
>            StreamWriter writer = new StreamWriter(stream);
>            writer.Write(body);
>
>            Attachment attachement = new Attachment(stream, ct);
>
>            msg.Attachments.Add(attachement);
>
>            smtp.Send(msg);
>            writer.Close();
>            stream.Close();
Author
5 Jan 2007 2:19 AM
James
Hi the writer.Flush() didn't help much... any ideas?

Show quote
"Henning Krause [MVP - Exchange]" wrote:

> Hello,
>
> do a
>
>     writer.Flush()
>
> right before calling the Send method..
>
> Best regards,
> Henning Krause
>
> "James" <Ja***@discussions.microsoft.com> wrote in message
> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> > Hi, I have been trying to create a file attachement straight from a stream
> > without touching the disk, however, the attachement always turns out to be
> > empty, please help!! I tried attach a existing file and it worked, however
> > I
> > really want to create the attachment on the fly. Here is my code,
> > thanks!!!
> >
> >            SmtpClient smtp = new SmtpClient();
> >            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
> >            smtp.Port =
> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
> >            {
> >                NetworkCredential cre = new
> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
> >                smtp.Credentials = cre;
> >            }
> >            MailMessage msg = new MailMessage();
> >            msg.IsBodyHtml = true;
> >            msg.To.Add(to);
> >            msg.From = new MailAddress(from);
> >            msg.Subject = subject;
> >            msg.Body = body;
> >
> >            ContentType ct = new ContentType();
> >            ct.MediaType = MediaTypeNames.Text.Plain;
> >            ct.Name = "InspectionReport.htm";
> >
> >            MemoryStream stream = new MemoryStream();
> >
> >            StreamWriter writer = new StreamWriter(stream);
> >            writer.Write(body);
> >
> >            Attachment attachement = new Attachment(stream, ct);
> >
> >            msg.Attachments.Add(attachement);
> >
> >            smtp.Send(msg);
> >            writer.Close();
> >            stream.Close();
>
>
Author
5 Jan 2007 2:29 AM
Nathan Alden
The problem may be threading? Perhaps Send() is trying to access the stream
at a later point, after stream.Close() has been called. In general it is not
necessary to Close() MemoryStreams because there is no unmanaged resource to
clean up. Try not closing the MemoryStream.

Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
> Hi the writer.Flush() didn't help much... any ideas?
>
> "Henning Krause [MVP - Exchange]" wrote:
>
>> Hello,
>>
>> do a
>>
>>     writer.Flush()
>>
>> right before calling the Send method..
>>
>> Best regards,
>> Henning Krause
>>
>> "James" <Ja***@discussions.microsoft.com> wrote in message
>> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
>> > Hi, I have been trying to create a file attachement straight from a
>> > stream
>> > without touching the disk, however, the attachement always turns out to
>> > be
>> > empty, please help!! I tried attach a existing file and it worked,
>> > however
>> > I
>> > really want to create the attachment on the fly. Here is my code,
>> > thanks!!!
>> >
>> >            SmtpClient smtp = new SmtpClient();
>> >            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
>> >            smtp.Port =
>> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
>> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
>> >            {
>> >                NetworkCredential cre = new
>> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
>> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
>> >                smtp.Credentials = cre;
>> >            }
>> >            MailMessage msg = new MailMessage();
>> >            msg.IsBodyHtml = true;
>> >            msg.To.Add(to);
>> >            msg.From = new MailAddress(from);
>> >            msg.Subject = subject;
>> >            msg.Body = body;
>> >
>> >            ContentType ct = new ContentType();
>> >            ct.MediaType = MediaTypeNames.Text.Plain;
>> >            ct.Name = "InspectionReport.htm";
>> >
>> >            MemoryStream stream = new MemoryStream();
>> >
>> >            StreamWriter writer = new StreamWriter(stream);
>> >            writer.Write(body);
>> >
>> >            Attachment attachement = new Attachment(stream, ct);
>> >
>> >            msg.Attachments.Add(attachement);
>> >
>> >            smtp.Send(msg);
>> >            writer.Close();
>> >            stream.Close();
>>
>>
Author
5 Jan 2007 2:41 AM
James
Hi Nathan,

I commented out the close methods, however same result. This is very annoying.

Show quote
"Nathan Alden" wrote:

> The problem may be threading? Perhaps Send() is trying to access the stream
> at a later point, after stream.Close() has been called. In general it is not
> necessary to Close() MemoryStreams because there is no unmanaged resource to
> clean up. Try not closing the MemoryStream.
>
> "James" <Ja***@discussions.microsoft.com> wrote in message
> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
> > Hi the writer.Flush() didn't help much... any ideas?
> >
> > "Henning Krause [MVP - Exchange]" wrote:
> >
> >> Hello,
> >>
> >> do a
> >>
> >>     writer.Flush()
> >>
> >> right before calling the Send method..
> >>
> >> Best regards,
> >> Henning Krause
> >>
> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> >> > Hi, I have been trying to create a file attachement straight from a
> >> > stream
> >> > without touching the disk, however, the attachement always turns out to
> >> > be
> >> > empty, please help!! I tried attach a existing file and it worked,
> >> > however
> >> > I
> >> > really want to create the attachment on the fly. Here is my code,
> >> > thanks!!!
> >> >
> >> >            SmtpClient smtp = new SmtpClient();
> >> >            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
> >> >            smtp.Port =
> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
> >> >            {
> >> >                NetworkCredential cre = new
> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
> >> >                smtp.Credentials = cre;
> >> >            }
> >> >            MailMessage msg = new MailMessage();
> >> >            msg.IsBodyHtml = true;
> >> >            msg.To.Add(to);
> >> >            msg.From = new MailAddress(from);
> >> >            msg.Subject = subject;
> >> >            msg.Body = body;
> >> >
> >> >            ContentType ct = new ContentType();
> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
> >> >            ct.Name = "InspectionReport.htm";
> >> >
> >> >            MemoryStream stream = new MemoryStream();
> >> >
> >> >            StreamWriter writer = new StreamWriter(stream);
> >> >            writer.Write(body);
> >> >
> >> >            Attachment attachement = new Attachment(stream, ct);
> >> >
> >> >            msg.Attachments.Add(attachement);
> >> >
> >> >            smtp.Send(msg);
> >> >            writer.Close();
> >> >            stream.Close();
> >>
> >>
>
>
>
Author
5 Jan 2007 3:02 AM
James
Also I tried to debug the method, and I found out the writer acutally does
write to the stream, because the size of the stream changed from 0 to
somethhing big. Correct me if I am wrong.

Show quote
"Nathan Alden" wrote:

> The problem may be threading? Perhaps Send() is trying to access the stream
> at a later point, after stream.Close() has been called. In general it is not
> necessary to Close() MemoryStreams because there is no unmanaged resource to
> clean up. Try not closing the MemoryStream.
>
> "James" <Ja***@discussions.microsoft.com> wrote in message
> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
> > Hi the writer.Flush() didn't help much... any ideas?
> >
> > "Henning Krause [MVP - Exchange]" wrote:
> >
> >> Hello,
> >>
> >> do a
> >>
> >>     writer.Flush()
> >>
> >> right before calling the Send method..
> >>
> >> Best regards,
> >> Henning Krause
> >>
> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> >> > Hi, I have been trying to create a file attachement straight from a
> >> > stream
> >> > without touching the disk, however, the attachement always turns out to
> >> > be
> >> > empty, please help!! I tried attach a existing file and it worked,
> >> > however
> >> > I
> >> > really want to create the attachment on the fly. Here is my code,
> >> > thanks!!!
> >> >
> >> >            SmtpClient smtp = new SmtpClient();
> >> >            smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
> >> >            smtp.Port =
> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] == "True")
> >> >            {
> >> >                NetworkCredential cre = new
> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
> >> >                smtp.Credentials = cre;
> >> >            }
> >> >            MailMessage msg = new MailMessage();
> >> >            msg.IsBodyHtml = true;
> >> >            msg.To.Add(to);
> >> >            msg.From = new MailAddress(from);
> >> >            msg.Subject = subject;
> >> >            msg.Body = body;
> >> >
> >> >            ContentType ct = new ContentType();
> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
> >> >            ct.Name = "InspectionReport.htm";
> >> >
> >> >            MemoryStream stream = new MemoryStream();
> >> >
> >> >            StreamWriter writer = new StreamWriter(stream);
> >> >            writer.Write(body);
> >> >
> >> >            Attachment attachement = new Attachment(stream, ct);
> >> >
> >> >            msg.Attachments.Add(attachement);
> >> >
> >> >            smtp.Send(msg);
> >> >            writer.Close();
> >> >            stream.Close();
> >>
> >>
>
>
>
Author
5 Jan 2007 7:49 AM
Henning Krause [MVP - Exchange]
Ah... thats the point here...

after you close the writer, do a stream.Seek(0, SeekOrigin.Begin).

This will reset the stream position to the beginning of the stream.

Best regards,
Henning Krause

Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:AC39E3B1-64A8-4CB9-9E99-F72B4FF0EFE1@microsoft.com...
> Also I tried to debug the method, and I found out the writer acutally does
> write to the stream, because the size of the stream changed from 0 to
> somethhing big. Correct me if I am wrong.
>
> "Nathan Alden" wrote:
>
>> The problem may be threading? Perhaps Send() is trying to access the
>> stream
>> at a later point, after stream.Close() has been called. In general it is
>> not
>> necessary to Close() MemoryStreams because there is no unmanaged resource
>> to
>> clean up. Try not closing the MemoryStream.
>>
>> "James" <Ja***@discussions.microsoft.com> wrote in message
>> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
>> > Hi the writer.Flush() didn't help much... any ideas?
>> >
>> > "Henning Krause [MVP - Exchange]" wrote:
>> >
>> >> Hello,
>> >>
>> >> do a
>> >>
>> >>     writer.Flush()
>> >>
>> >> right before calling the Send method..
>> >>
>> >> Best regards,
>> >> Henning Krause
>> >>
>> >> "James" <Ja***@discussions.microsoft.com> wrote in message
>> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
>> >> > Hi, I have been trying to create a file attachement straight from a
>> >> > stream
>> >> > without touching the disk, however, the attachement always turns out
>> >> > to
>> >> > be
>> >> > empty, please help!! I tried attach a existing file and it worked,
>> >> > however
>> >> > I
>> >> > really want to create the attachment on the fly. Here is my code,
>> >> > thanks!!!
>> >> >
>> >> >            SmtpClient smtp = new SmtpClient();
>> >> >            smtp.Host =
>> >> > ConfigurationManager.AppSettings["SMTP_HOST"];
>> >> >            smtp.Port =
>> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
>> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] ==
>> >> > "True")
>> >> >            {
>> >> >                NetworkCredential cre = new
>> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
>> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
>> >> >                smtp.Credentials = cre;
>> >> >            }
>> >> >            MailMessage msg = new MailMessage();
>> >> >            msg.IsBodyHtml = true;
>> >> >            msg.To.Add(to);
>> >> >            msg.From = new MailAddress(from);
>> >> >            msg.Subject = subject;
>> >> >            msg.Body = body;
>> >> >
>> >> >            ContentType ct = new ContentType();
>> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
>> >> >            ct.Name = "InspectionReport.htm";
>> >> >
>> >> >            MemoryStream stream = new MemoryStream();
>> >> >
>> >> >            StreamWriter writer = new StreamWriter(stream);
>> >> >            writer.Write(body);
>> >> >
>> >> >            Attachment attachement = new Attachment(stream, ct);
>> >> >
>> >> >            msg.Attachments.Add(attachement);
>> >> >
>> >> >            smtp.Send(msg);
>> >> >            writer.Close();
>> >> >            stream.Close();
>> >>
>> >>
>>
>>
>>
Author
7 Jan 2007 10:13 AM
James
thanks for the reply, it works!!! I should write up a tutorial for how to do
it, since there isn't one.

Show quote
"Henning Krause [MVP - Exchange]" wrote:

> Ah... thats the point here...
>
> after you close the writer, do a stream.Seek(0, SeekOrigin.Begin).
>
> This will reset the stream position to the beginning of the stream.
>
> Best regards,
> Henning Krause
>
> "James" <Ja***@discussions.microsoft.com> wrote in message
> news:AC39E3B1-64A8-4CB9-9E99-F72B4FF0EFE1@microsoft.com...
> > Also I tried to debug the method, and I found out the writer acutally does
> > write to the stream, because the size of the stream changed from 0 to
> > somethhing big. Correct me if I am wrong.
> >
> > "Nathan Alden" wrote:
> >
> >> The problem may be threading? Perhaps Send() is trying to access the
> >> stream
> >> at a later point, after stream.Close() has been called. In general it is
> >> not
> >> necessary to Close() MemoryStreams because there is no unmanaged resource
> >> to
> >> clean up. Try not closing the MemoryStream.
> >>
> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
> >> > Hi the writer.Flush() didn't help much... any ideas?
> >> >
> >> > "Henning Krause [MVP - Exchange]" wrote:
> >> >
> >> >> Hello,
> >> >>
> >> >> do a
> >> >>
> >> >>     writer.Flush()
> >> >>
> >> >> right before calling the Send method..
> >> >>
> >> >> Best regards,
> >> >> Henning Krause
> >> >>
> >> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> >> >> > Hi, I have been trying to create a file attachement straight from a
> >> >> > stream
> >> >> > without touching the disk, however, the attachement always turns out
> >> >> > to
> >> >> > be
> >> >> > empty, please help!! I tried attach a existing file and it worked,
> >> >> > however
> >> >> > I
> >> >> > really want to create the attachment on the fly. Here is my code,
> >> >> > thanks!!!
> >> >> >
> >> >> >            SmtpClient smtp = new SmtpClient();
> >> >> >            smtp.Host =
> >> >> > ConfigurationManager.AppSettings["SMTP_HOST"];
> >> >> >            smtp.Port =
> >> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
> >> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] ==
> >> >> > "True")
> >> >> >            {
> >> >> >                NetworkCredential cre = new
> >> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> >> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
> >> >> >                smtp.Credentials = cre;
> >> >> >            }
> >> >> >            MailMessage msg = new MailMessage();
> >> >> >            msg.IsBodyHtml = true;
> >> >> >            msg.To.Add(to);
> >> >> >            msg.From = new MailAddress(from);
> >> >> >            msg.Subject = subject;
> >> >> >            msg.Body = body;
> >> >> >
> >> >> >            ContentType ct = new ContentType();
> >> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
> >> >> >            ct.Name = "InspectionReport.htm";
> >> >> >
> >> >> >            MemoryStream stream = new MemoryStream();
> >> >> >
> >> >> >            StreamWriter writer = new StreamWriter(stream);
> >> >> >            writer.Write(body);
> >> >> >
> >> >> >            Attachment attachement = new Attachment(stream, ct);
> >> >> >
> >> >> >            msg.Attachments.Add(attachement);
> >> >> >
> >> >> >            smtp.Send(msg);
> >> >> >            writer.Close();
> >> >> >            stream.Close();
> >> >>
> >> >>
> >>
> >>
> >>
>
>
Author
5 Jan 2007 12:14 PM
Kevin Spencer
Hi James,

It is necessary to seek to the beginning of the stream prior to sending it,
and to make sure that that the stream is not closed or disposed until the
mail is sent.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.

Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:AC39E3B1-64A8-4CB9-9E99-F72B4FF0EFE1@microsoft.com...
> Also I tried to debug the method, and I found out the writer acutally does
> write to the stream, because the size of the stream changed from 0 to
> somethhing big. Correct me if I am wrong.
>
> "Nathan Alden" wrote:
>
>> The problem may be threading? Perhaps Send() is trying to access the
>> stream
>> at a later point, after stream.Close() has been called. In general it is
>> not
>> necessary to Close() MemoryStreams because there is no unmanaged resource
>> to
>> clean up. Try not closing the MemoryStream.
>>
>> "James" <Ja***@discussions.microsoft.com> wrote in message
>> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
>> > Hi the writer.Flush() didn't help much... any ideas?
>> >
>> > "Henning Krause [MVP - Exchange]" wrote:
>> >
>> >> Hello,
>> >>
>> >> do a
>> >>
>> >>     writer.Flush()
>> >>
>> >> right before calling the Send method..
>> >>
>> >> Best regards,
>> >> Henning Krause
>> >>
>> >> "James" <Ja***@discussions.microsoft.com> wrote in message
>> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
>> >> > Hi, I have been trying to create a file attachement straight from a
>> >> > stream
>> >> > without touching the disk, however, the attachement always turns out
>> >> > to
>> >> > be
>> >> > empty, please help!! I tried attach a existing file and it worked,
>> >> > however
>> >> > I
>> >> > really want to create the attachment on the fly. Here is my code,
>> >> > thanks!!!
>> >> >
>> >> >            SmtpClient smtp = new SmtpClient();
>> >> >            smtp.Host =
>> >> > ConfigurationManager.AppSettings["SMTP_HOST"];
>> >> >            smtp.Port =
>> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
>> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] ==
>> >> > "True")
>> >> >            {
>> >> >                NetworkCredential cre = new
>> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
>> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
>> >> >                smtp.Credentials = cre;
>> >> >            }
>> >> >            MailMessage msg = new MailMessage();
>> >> >            msg.IsBodyHtml = true;
>> >> >            msg.To.Add(to);
>> >> >            msg.From = new MailAddress(from);
>> >> >            msg.Subject = subject;
>> >> >            msg.Body = body;
>> >> >
>> >> >            ContentType ct = new ContentType();
>> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
>> >> >            ct.Name = "InspectionReport.htm";
>> >> >
>> >> >            MemoryStream stream = new MemoryStream();
>> >> >
>> >> >            StreamWriter writer = new StreamWriter(stream);
>> >> >            writer.Write(body);
>> >> >
>> >> >            Attachment attachement = new Attachment(stream, ct);
>> >> >
>> >> >            msg.Attachments.Add(attachement);
>> >> >
>> >> >            smtp.Send(msg);
>> >> >            writer.Close();
>> >> >            stream.Close();
>> >>
>> >>
>>
>>
>>
Author
7 Jan 2007 10:12 AM
James
Hi, stream.seek did the job, also the msg.dispose method will release all the
resource also close the stream as well. thanks guys.

Show quote
"Kevin Spencer" wrote:

> Hi James,
>
> It is necessary to seek to the beginning of the stream prior to sending it,
> and to make sure that that the stream is not closed or disposed until the
> mail is sent.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Bit Player
> http://unclechutney.blogspot.com
>
> Where there's a Will, there's a William.
>
> "James" <Ja***@discussions.microsoft.com> wrote in message
> news:AC39E3B1-64A8-4CB9-9E99-F72B4FF0EFE1@microsoft.com...
> > Also I tried to debug the method, and I found out the writer acutally does
> > write to the stream, because the size of the stream changed from 0 to
> > somethhing big. Correct me if I am wrong.
> >
> > "Nathan Alden" wrote:
> >
> >> The problem may be threading? Perhaps Send() is trying to access the
> >> stream
> >> at a later point, after stream.Close() has been called. In general it is
> >> not
> >> necessary to Close() MemoryStreams because there is no unmanaged resource
> >> to
> >> clean up. Try not closing the MemoryStream.
> >>
> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> news:B7E8976C-2BFE-4B50-8BA9-CD809A64D4C5@microsoft.com...
> >> > Hi the writer.Flush() didn't help much... any ideas?
> >> >
> >> > "Henning Krause [MVP - Exchange]" wrote:
> >> >
> >> >> Hello,
> >> >>
> >> >> do a
> >> >>
> >> >>     writer.Flush()
> >> >>
> >> >> right before calling the Send method..
> >> >>
> >> >> Best regards,
> >> >> Henning Krause
> >> >>
> >> >> "James" <Ja***@discussions.microsoft.com> wrote in message
> >> >> news:D9888B96-6B12-491B-852F-0CA212B6F40A@microsoft.com...
> >> >> > Hi, I have been trying to create a file attachement straight from a
> >> >> > stream
> >> >> > without touching the disk, however, the attachement always turns out
> >> >> > to
> >> >> > be
> >> >> > empty, please help!! I tried attach a existing file and it worked,
> >> >> > however
> >> >> > I
> >> >> > really want to create the attachment on the fly. Here is my code,
> >> >> > thanks!!!
> >> >> >
> >> >> >            SmtpClient smtp = new SmtpClient();
> >> >> >            smtp.Host =
> >> >> > ConfigurationManager.AppSettings["SMTP_HOST"];
> >> >> >            smtp.Port =
> >> >> > Int32.Parse(ConfigurationManager.AppSettings["SMTP_PORT"]);
> >> >> >            if (ConfigurationManager.AppSettings["SMTP_AUTH"] ==
> >> >> > "True")
> >> >> >            {
> >> >> >                NetworkCredential cre = new
> >> >> > NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"],
> >> >> > ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
> >> >> >                smtp.Credentials = cre;
> >> >> >            }
> >> >> >            MailMessage msg = new MailMessage();
> >> >> >            msg.IsBodyHtml = true;
> >> >> >            msg.To.Add(to);
> >> >> >            msg.From = new MailAddress(from);
> >> >> >            msg.Subject = subject;
> >> >> >            msg.Body = body;
> >> >> >
> >> >> >            ContentType ct = new ContentType();
> >> >> >            ct.MediaType = MediaTypeNames.Text.Plain;
> >> >> >            ct.Name = "InspectionReport.htm";
> >> >> >
> >> >> >            MemoryStream stream = new MemoryStream();
> >> >> >
> >> >> >            StreamWriter writer = new StreamWriter(stream);
> >> >> >            writer.Write(body);
> >> >> >
> >> >> >            Attachment attachement = new Attachment(stream, ct);
> >> >> >
> >> >> >            msg.Attachments.Add(attachement);
> >> >> >
> >> >> >            smtp.Send(msg);
> >> >> >            writer.Close();
> >> >> >            stream.Close();
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>

AddThis Social Bookmark Button