Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ContentType Constructor

Initializes a new default instance of the ContentType class.

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

This constructor initializes the MediaType property to "application/octet-stream".

The following code example demonstrates how to call this constructor.

		// 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 logMailer@contoso.com to recipientList.
			MailMessage message = new MailMessage(
			   "logMailer@contoso.com", 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

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.