我有一种将文件作为附件发送的方法.我使用StreamWriter
和MemoryStream
创建附件.代码如下:
public void ComposeEmail(string from, string to, SmtpClient client)
{
MailMessage mm = new MailMessage(from, to, "Otrzymałeś nowe zamówienie od "+from , "Przesyłam nowe zamówienie na sprzęt");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
// Adding attachment:
using (var ms = new System.IO.MemoryStream())
{
using (var writer = new System.IO.StreamWriter(ms))
{
writer.Write("Hello its my sample file");
writer.Flush();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.txt";
mm.Attachments.Add(attach);
try
{
client.Send(mm);
}
catch (SmtpException e)
{
Console.WriteLine(e.ToString());
}
}
}
}
如您在这些行中看到的,我将写入"文件":
writer.Write("Hello its my sample file");
writer.Flush();
在调试时,我可以看到它的MemoryStream
长度为24(就像写入字符串的长度一样).但是邮箱中收到的文件为空.
我究竟做错了什么?
Flush
上StreamWriter
是不(相当)冗余的; 在某些情况下会添加数据尝试倒带流:
writer.Flush();
ms.Position = 0; // <===== here
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(
System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
否则,流仍将定位在最后,从那里读取将立即报告EOF.
StreamWriter.Flush()
,但实际上并非如此.当然可以.但是情况.I have a method in which I send File as attachment.
I use StreamWriter
and MemoryStream
to create attachment.
Code below:
public void ComposeEmail(string from, string to, SmtpClient client)
{
MailMessage mm = new MailMessage(from, to, "Otrzymałeś nowe zamówienie od "+from , "Przesyłam nowe zamówienie na sprzęt");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
// Adding attachment:
using (var ms = new System.IO.MemoryStream())
{
using (var writer = new System.IO.StreamWriter(ms))
{
writer.Write("Hello its my sample file");
writer.Flush();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.txt";
mm.Attachments.Add(attach);
try
{
client.Send(mm);
}
catch (SmtpException e)
{
Console.WriteLine(e.ToString());
}
}
}
}
as You can see in these lines I write to "file":
writer.Write("Hello its my sample file");
writer.Flush();
While debugging I can see that MemoryStream
has length of 24 ( just like length of string written to it).
But file received in mailbox is empty.
What am I doing wrong?
Flush
on StreamWriter
is not (quite) redundant; there are scenarios where it adds dataTry rewinding the stream:
writer.Flush();
ms.Position = 0; // <===== here
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(
System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
Otherwise, the stream is still going to be positioned at the end, and reading from there will immediately report EOF.
StreamWriter.Flush()
, and it really isn't. Edge cases, sure. But cases.本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文