Web应用中实现发送带附件的电子邮件

首先关注MSDN上关于Attachment的两个解释:

Attachment Constructor

First of all, the Stream that you supply to an attachment is not read until you actually send a message. That should make sense enough, but sometimes it's easy to forget. If your stream relies on some other resource that was disposed of by the time the message is sent, the client will fail to send your message.

Secondly, if you are writing into a Stream that will later be read by an Attachment, make sure you set the Position back to 0. Otherwise, you may end up with zero-length attachments even though your stream has data! Attachment does not appear to rewind the Stream for you; it reads from whatever position the Stream is currently at.

针对第二种解释,我在测试过程中MemoryStream.Seek(0,SeekOrigin.Begin),可是附件的长度还是为0,不知道为什么;等有时间再测试一下。

解释:MemoryStream.Write()后,流的当前位置是停在结束位置上,如果再Read(),是从结束位开始读.正确的做法应该在Read() 前重新设置流位置MemoryStream.Seek(0,SeekOrigin.Begin),

现在是变通一下,先发送后删除。

 

Code


 

你可能感兴趣的:(Web应用)