c#中的电子邮件附件

时间:2015-03-18 07:23:17

标签: c# .net asp.net-mvc

我将以下代码作为我的电子邮件附件代码。但它给我一个错误,我无法弄清楚来源。所有处置行都抛出错误,new Attachment(FileAttachment, MediaTypeNames.Application.Octet);也是

    if (FileAttachment != null){
        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(FileAttachment, MediaTypeNames.Application.Octet);
       // Add time stamp information for the file.
       ContentDisposition disposition = data.ContentDisposition;
       disposition.CreationDate = System.IO.File.GetCreationTime(FileAttachment);
       disposition.ModificationDate = System.IO.File.GetLastWriteTime(FileAttachment);
       disposition.ReadDate = System.IO.File.GetLastAccessTime(FileAttachment);
       // Add the file attachment to this e-mail message.
       AllMailMessage.Attachments.Add(data);
    }

该行的#rorr消息"新附件(FileAttachment,MediaTypeNames.Application.Octet);"

Class System.Net.Mime.Media Typenames
specifies the media type information for email message attachment

Error:

The best overloaded method match for "System.Net.Mail.Attachment.Attachment..." has some invalid arguments

对于IO.File的三行,即System.IO.File.GetLastWriteTime(FileAttachment);

错误是相同的

The best overloaded method match for "System.IO.File...." has some invalid arguments

asp.Net文件上载代码

<td class="FileUpload">
                    <asp:FileUpload ID="FileAttachment" runat="server" />
                </td>

3 个答案:

答案 0 :(得分:1)

附件构造函数无法获取FileUpload作为参数。它需要StringStream。 您可以从FileContent的{​​{1}}属性获取上传的文件流。

FileUpload

答案 1 :(得分:0)

问题是FileUpload无法作为Attachment类的参数

有关Attachment

的定义,请参阅:https://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx

您需要将Attachment中的文件转换为要用作参数的流

答案 2 :(得分:0)

使用此...

mail.Attachments.Add(new Attachment(@"C:\ws_Log.txt"));