ContentType.Name Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the value of the name parameter included in the Content-Type header represented by this instance.

Namespace:   System.Net.Mime
Assembly:  System (in System.dll)

public string Name { get; set; }

Property Value

Type: System.String

A String that contains the value associated with the name parameter.

In the following example of a Content-Type header, the value of the Name property is "".

content-type: application/x-myType;

A grammar that details the syntax of the Content-Type header is described in RFC 2045 Section 5.1, available at

When specifying content for a Attachment as a Stream or String, you can use Name to set the name of the file that stores the content on the recipient's system.

The following code example sets the value of the property to specify a name for a file being attached as a stream.

// The following example sends a summary of a log file as the message
// and the log as an e-mail attachment.
public static void SendNamedErrorLog(string server, string recipientList)
	// Create a message from to recipientList.
	MailMessage message = new MailMessage(
	   "", recipientList);

	message.Subject = "Error Log report";
	string fileName = "log.txt";
	// Get the file stream for the error log.
	// Requires the System.IO namespace.
	FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
	StreamReader s = new StreamReader(fs);
	int errors = 0;
	while (s.ReadLine() != null)
		// Process each line from the log file here.
	// The e-mail message summarizes the data found in the log.
	message.Body = String.Format("{0} errors in log as of {1}",
		errors, DateTime.Now);
	// Close the stream reader. This also closes the file.
	// Re-open the file at the beginning to make the attachment.
	fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
	// Make a ContentType indicating that the log data
	// that is attached is plain text and is named.
	ContentType ct = new ContentType();
	ct.MediaType = MediaTypeNames.Text.Plain;
	ct.Name = "log" + DateTime.Now.ToString() + ".txt";
	// Create the attachment.
	Attachment data = new Attachment(fs, ct);
	// Add the attachment to the message.
	// Send the message.
	// Include credentials if the server requires them.
	SmtpClient client = new SmtpClient(server);
	client.Credentials = CredentialCache.DefaultNetworkCredentials;

   try {
    catch (Exception ex) {
      Console.WriteLine("Exception caught in SendNamedErrorLog: {0}", 
                  ex.ToString() );
	// Close the log file.

.NET Framework
Available since 2.0
Return to top