Aracılığıyla paylaş


ContentType Oluşturucular

Tanım

ContentType sınıfının yeni bir örneğini başlatır.

Aşırı Yüklemeler

Name Description
ContentType()

Sınıfının yeni bir varsayılan örneğini ContentType başlatır.

ContentType(String)

Belirtilen dizeyi ContentType kullanarak sınıfının yeni bir örneğini başlatır.

ContentType()

Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs

Sınıfının yeni bir varsayılan örneğini ContentType başlatır.

public:
 ContentType();
public ContentType();
Public Sub New ()

Örnekler

Aşağıdaki kod örneği, bu oluşturucunun nasıl çağrılduğunu gösterir.

// The following example sends a summary of a log file as the message
// and the log as an email 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.
        errors++;
    }
    // The email 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.
    s.Close();
    // 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.
    message.Attachments.Add(data);
    // Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
    return;
}

Açıklamalar

Bu oluşturucu özelliğini olarak "application/octet-stream"başlatırMediaType.

Şunlara uygulanır

ContentType(String)

Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs
Kaynak:
ContentType.cs

Belirtilen dizeyi ContentType kullanarak sınıfının yeni bir örneğini başlatır.

public:
 ContentType(System::String ^ contentType);
public ContentType(string contentType);
new System.Net.Mime.ContentType : string -> System.Net.Mime.ContentType
Public Sub New (contentType As String)

Parametreler

contentType
String

StringÖRNEĞIN, "text/plain; charset=us-ascii"MIME medya türünü, alt türünü ve isteğe bağlı parametreleri içeren bir .

Özel durumlar

contentType, null'e eşittir.

contentType ("") şeklindedir Empty .

contentType ayrıştırılamayan bir biçimdedir.

Örnekler

Aşağıdaki kod örneği, bu oluşturucunun nasıl çağrılduğunu gösterir.

// The following example sends a summary of a log file as the message
// and the log as an email 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.
        errors++;
    }
    // The email 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.
    s.Close();
    // 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.
    message.Attachments.Add(data);
    // Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
    return;
}

Açıklamalar

Dizenin contentType söz dizimi RFC 2045 Bölüm 5.1'de https://www.ietf.orgaçıklanmıştır.

Şunlara uygulanır