从邮件附着物C#获取文件名 [英] C# Get filename from mail attachements

查看:185
本文介绍了从邮件附着物C#获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的C#应用​​程序,发送电子邮件的SMTP(使用System.Net.Mail类)。发送(电子邮件)MAILMESSAGE一个对象我希望通过附件列表进行迭代并删除与这些附件相关联的原始文件...但我有一个很难找到与每个附件关联的完整的文件路径之后 - 没有保留我的自己收藏的附件文件路径的。这里一定是来提取附件对象的完整的文件路径的好方法。

I have a simple C# app that send SMTP emails (using System.Net.Mail classes). After sending (emailing) a MailMessage object I want to iterate through the list of the attachments and delete the original files associated with those attachments... but I am having a hard time finding the full file path associated with each attachment - without keeping my own collection of attachment filepaths. There has got to be a good way to extract the full file path from the attachment object.

我知道这一定是简单的,但我花办法不多时间this..time要求别人。

I know this has got to be simple, but I am spending way to much time on this..time to ask others.

推荐答案

可以


  • 附件.ContentStream

  • 如果您现在有一个StreamReader或类似的,使用的 BaseStream 财产,试图找到内在的FileStream

  • 阅读的 FileStream.Name

  • Read Attachment.ContentStream
  • If you now have a StreamReader or similar, use the BaseStream property to try and find the inner FileStream
  • Read FileStream.Name

但请记住,邮件消息(因此附件和流)可能不会被收集或立即清理,所以你可能无法删除该文件,立竿见影。你可能会做的更好子类附件,并双双创下的文件名和子类的Dispose(基本处置后执行)做删除,如果你真的需要这样做事。

but bear in mind that the mail message (and hence attachments and their streams) may not get collected or cleaned up immediately, so you may not be able to delete the file straight away. You might do better subclassing Attachment and both record the filename and subclass Dispose (to execute after the base dispose) to do the delete if you really do need to do things this way.

这篇关于从邮件附着物C#获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆