Condividi tramite


Attachment Costruttori

Definizione

Inizializza una nuova istanza della classe Attachment.

Overload

Nome Descrizione
Attachment(String)

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto specificata.

Attachment(Stream, ContentType)

Inizializza una nuova istanza della Attachment classe con il flusso e il tipo di contenuto specificati.

Attachment(Stream, String)

Inizializza una nuova istanza della Attachment classe con il flusso e il nome specificati.

Attachment(String, ContentType)

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto specificata e ContentType.

Attachment(String, String)

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto e le informazioni sul tipo MIME specificate.

Attachment(Stream, String, String)

Inizializza una nuova istanza della Attachment classe con il flusso, il nome e le informazioni sul tipo MIME specificati.

Attachment(String)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto specificata.

public:
 Attachment(System::String ^ fileName);
public Attachment(string fileName);
new System.Net.Mail.Attachment : string -> System.Net.Mail.Attachment
Public Sub New (fileName As String)

Parametri

fileName
String

Oggetto String contenente un percorso di file da utilizzare per creare questo allegato.

Eccezioni

fileName è null.

fileName è vuoto.

Esempio

Nell'esempio di codice seguente viene illustrato come chiamare questo costruttore.

public static void CreateMessageInlineAttachment2(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);
    // Send textMessage as part of the email body.
    message.Attachments.Add(data);
    ContentType content = data.ContentType;
    content.MediaType = MediaTypeNames.Text.Plain;
    //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 CreateMessageInlineAttachment2: {0}",
            ex.ToString());
    }
    data.Dispose();
}

Commenti

Le proprietà vengono impostate nel modo seguente:

Proprietà Valore
MediaType Plain.
TransferEncoding QuotedPrintable.

Si applica a

Attachment(Stream, ContentType)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con il flusso e il tipo di contenuto specificati.

public:
 Attachment(System::IO::Stream ^ contentStream, System::Net::Mime::ContentType ^ contentType);
public Attachment(System.IO.Stream contentStream, System.Net.Mime.ContentType contentType);
new System.Net.Mail.Attachment : System.IO.Stream * System.Net.Mime.ContentType -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, contentType As ContentType)

Parametri

contentStream
Stream

Oggetto leggibile Stream che contiene il contenuto per questo allegato.

contentType
ContentType

Oggetto ContentType che descrive i dati in contentStream.

Eccezioni

contentType è null.

oppure

contentStream è null.

Esempio

Nell'esempio di codice seguente viene illustrato come chiamare questo costruttore.

// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendErrorLog(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.
    ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
    // Attach the log file stream to the email message.
    Attachment data = new Attachment(fs, ct);
    ContentDisposition disposition = data.ContentDisposition;
    // Suggest a file name for the attachment.
    disposition.FileName = "log" + DateTime.Now.ToString() + ".txt";
    // 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 SendErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

Commenti

La proprietà TransferEncoding è impostata su Base64.

Se la proprietà del CanSeek flusso è false, l'allegato e l'oggetto MailMessage che lo contiene non sono riutilizzabili. È necessario fornire un flusso che può essere cercato per riutilizzare un allegato.

Si applica a

Attachment(Stream, String)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con il flusso e il nome specificati.

public:
 Attachment(System::IO::Stream ^ contentStream, System::String ^ name);
public Attachment(System.IO.Stream contentStream, string? name);
public Attachment(System.IO.Stream contentStream, string name);
new System.Net.Mail.Attachment : System.IO.Stream * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String)

Parametri

contentStream
Stream

Oggetto leggibile Stream che contiene il contenuto per questo allegato.

name
String

Oggetto String contenente il valore per la Name proprietà dell'oggetto ContentType associato a questo allegato. Il valore può essere null.

Eccezioni

contentStream è null.

Esempio

Nell'esempio di codice seguente viene illustrato come chiamare questo costruttore.

// 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;
}

Commenti

Se name non null è o uguale a String.Empty (""), l'oggetto ContentType per questo allegato viene costruito con la Name proprietà impostata su name. La proprietà TransferEncoding è impostata su Base64.

Se la proprietà del CanSeek flusso è false, l'allegato e l'oggetto MailMessage che lo contiene non sono riutilizzabili. È necessario fornire un flusso che può essere cercato per riutilizzare un allegato.

Si applica a

Attachment(String, ContentType)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto specificata e ContentType.

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

Parametri

fileName
String

Oggetto String contenente un percorso di file da utilizzare per creare questo allegato.

contentType
ContentType

Oggetto ContentType che descrive i dati in fileName.

Eccezioni

fileName è null.

contentType non è nel formato corretto.

Si applica a

Attachment(String, String)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con la stringa di contenuto e le informazioni sul tipo MIME specificate.

public:
 Attachment(System::String ^ fileName, System::String ^ mediaType);
public Attachment(string fileName, string? mediaType);
public Attachment(string fileName, string mediaType);
new System.Net.Mail.Attachment : string * string -> System.Net.Mail.Attachment
Public Sub New (fileName As String, mediaType As String)

Parametri

fileName
String

Oggetto String che contiene il contenuto per questo allegato.

mediaType
String

Oggetto String contenente le informazioni sull'intestazione del contenuto MIME per questo allegato. Il valore può essere null.

Eccezioni

fileName è null.

mediaType non è nel formato corretto.

Esempio

Nell'esempio di codice seguente viene illustrato come chiamare questo costruttore.

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

    // Attach the message string to this email message.
    Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
    // Send textMessage as part of the email body.
    message.Attachments.Add(data);
    ContentDisposition disposition = data.ContentDisposition;
    disposition.Inline = true;
    //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 CreateMessageInlineAttachment: {0}",
            ex.ToString());
    }
    data.Dispose();
}

Commenti

Se mediaType è null o uguale a String.Empty (""), la MediaType proprietà per questo allegato è impostata su Plain. Se mediaType non null è e non è una stringa di lunghezza zero, viene usata per costruire l'oggetto ContentType associato a questo allegato.

Si applica a

Attachment(Stream, String, String)

Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs
Origine:
Attachment.cs

Inizializza una nuova istanza della Attachment classe con il flusso, il nome e le informazioni sul tipo MIME specificati.

public:
 Attachment(System::IO::Stream ^ contentStream, System::String ^ name, System::String ^ mediaType);
public Attachment(System.IO.Stream contentStream, string? name, string? mediaType);
public Attachment(System.IO.Stream contentStream, string name, string mediaType);
new System.Net.Mail.Attachment : System.IO.Stream * string * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String, mediaType As String)

Parametri

contentStream
Stream

Oggetto leggibile Stream che contiene il contenuto per questo allegato.

name
String

Oggetto String contenente il valore per la Name proprietà dell'oggetto ContentType associato a questo allegato. Il valore può essere null.

mediaType
String

Oggetto String contenente le informazioni sull'intestazione del contenuto MIME per questo allegato. Il valore può essere null.

Eccezioni

contentStream è null.

mediaType non è nel formato corretto.

Esempio

Nell'esempio di codice seguente viene illustrato come chiamare questo costruttore.

// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedAndTypedErrorLog(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);
    // Create a name for the log data file.
    string name = "log" + DateTime.Now.ToString() + ".txt";
    // Create the attachment, name it, and specify the MIME type.
    Attachment data = new Attachment(fs, name, MediaTypeNames.Text.Plain);
    // 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 SendNamedAndTypedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

Commenti

Se mediaType non null è o uguale a String.Empty (""), viene usato per costruire la ContentType classe associata a questo allegato.

Se mediaType e name entrambi contengono Name informazioni, viene usato il valore specificato in name . La proprietà TransferEncoding è impostata su Base64.

Se la proprietà del CanSeek flusso è false, l'allegato e l'oggetto MailMessage che lo contiene non sono riutilizzabili. È necessario fornire un flusso che può essere cercato per riutilizzare un allegato.

Si applica a