语言

ContentDisposition.FileName 属性

定义

获取或设置电子邮件附件的建议文件名。

public:
 property System::String ^ FileName { System::String ^ get(); void set(System::String ^ value); };
public string? FileName { get; set; }
public string FileName { get; set; }
member this.FileName : string with get, set
Public Property FileName As String

属性值

包含文件名的 A String 。

示例

下面的代码示例演示如何设置此属性的值。

public static void CreateMessageAttachment1(string server, string textMessage)
{
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "jane@contoso.com",
       "ben@contoso.com",
       "A text message for you.",
       "Message: ");

    // Attach the message string to this email message.
    Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
    ContentDisposition disposition = data.ContentDisposition;
    // Suggest a file name for the attachment.
    disposition.FileName = "message" + DateTime.Now.ToString();
    message.Attachments.Add(data);
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageAttachment1(): {0}",
            ex.ToString());
    }
    // Display the values in the ContentDisposition for the attachment.
    Console.WriteLine("Content disposition");
    Console.WriteLine(disposition.ToString());
    Console.WriteLine("File {0}", disposition.FileName);
    Console.WriteLine("Size {0}", disposition.Size);
    Console.WriteLine("Creation {0}", disposition.CreationDate);
    Console.WriteLine("Modification {0}", disposition.ModificationDate);
    Console.WriteLine("Read {0}", disposition.ReadDate);
    Console.WriteLine("Inline {0}", disposition.Inline);
    Console.WriteLine("Parameters: {0}", disposition.Parameters.Count);
    foreach (DictionaryEntry d in disposition.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}

注解

该 FileName 属性允许发件人建议名称用于将电子邮件附件存储在收件人的计算机上。 此名称只是建议;接收系统可以忽略它。 名称不得包含路径信息;接收计算机忽略任何此类信息。

若要删除文件名信息,可以将此属性 null 设置为空字符串或空字符串(“)。

Content-Disposition 标头在 RFC 2183 中 https://www.ietf.org提供。

适用于