Partager via


Attachment Constructeurs

Définition

Initialise une nouvelle instance de la classe Attachment.

Surcharges

Nom Description
Attachment(String)

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée.

Attachment(Stream, ContentType)

Initialise une nouvelle instance de la Attachment classe avec le flux et le type de contenu spécifiés.

Attachment(Stream, String)

Initialise une nouvelle instance de la Attachment classe avec le flux et le nom spécifiés.

Attachment(String, ContentType)

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée et ContentType.

Attachment(String, String)

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée et les informations de type MIME.

Attachment(Stream, String, String)

Initialise une nouvelle instance de la Attachment classe avec le flux, le nom et les informations de type MIME spécifiés.

Attachment(String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée.

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)

Paramètres

fileName
String

Qui String contient un chemin d’accès de fichier à utiliser pour créer cette pièce jointe.

Exceptions

fileName a la valeur null.

fileName est vide.

Exemples

L’exemple de code suivant montre comment appeler ce constructeur.

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();
}

Remarques

Les propriétés sont définies comme suit :

Propriété Valeur
MediaType Plain.
TransferEncoding QuotedPrintable.

S’applique à

Attachment(Stream, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec le flux et le type de contenu spécifiés.

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)

Paramètres

contentStream
Stream

Accessible en lecture Stream qui contient le contenu de cette pièce jointe.

contentType
ContentType

Qui ContentType décrit les données dans contentStream.

Exceptions

contentType a la valeur null.

- ou -

contentStream a la valeur null.

Exemples

L’exemple de code suivant montre comment appeler ce constructeur.

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

Remarques

La TransferEncoding propriété est définie sur Base64.

Si la propriété du CanSeek flux est false, la pièce jointe et celle MailMessage qui le contient ne sont pas réutilisables. Vous devez fournir un flux pouvant être recherché pour réutiliser une pièce jointe.

S’applique à

Attachment(Stream, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec le flux et le nom spécifiés.

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)

Paramètres

contentStream
Stream

Accessible en lecture Stream qui contient le contenu de cette pièce jointe.

name
String

Valeur String qui contient la valeur de la Name propriété associée ContentType à cette pièce jointe. Cette valeur peut être null.

Exceptions

contentStream a la valeur null.

Exemples

L’exemple de code suivant montre comment appeler ce constructeur.

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

Remarques

S’il name n’est pas null ou égal à String.Empty (« »), la ContentType valeur de cette pièce jointe est construite avec la Name propriété définie namesur . La TransferEncoding propriété est définie sur Base64.

Si la propriété du CanSeek flux est false, la pièce jointe et celle MailMessage qui le contient ne sont pas réutilisables. Vous devez fournir un flux qui peut être recherché pour réutiliser une pièce jointe.

S’applique à

Attachment(String, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée et 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)

Paramètres

fileName
String

Qui String contient un chemin d’accès de fichier à utiliser pour créer cette pièce jointe.

contentType
ContentType

Qui ContentType décrit les données dans fileName.

Exceptions

fileName a la valeur null.

contentType n’est pas au format correct.

S’applique à

Attachment(String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec la chaîne de contenu spécifiée et les informations de type MIME.

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)

Paramètres

fileName
String

Qui String contient le contenu de cette pièce jointe.

mediaType
String

Qui String contient les informations d’en-tête de contenu MIME pour cette pièce jointe. Cette valeur peut être null.

Exceptions

fileName a la valeur null.

mediaType n’est pas au format correct.

Exemples

L’exemple de code suivant montre comment appeler ce constructeur.

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();
}

Remarques

Si mediaType elle est null ou égale à String.Empty ( » « ), la MediaType propriété de cette pièce jointe est définie sur Plain. Si mediaType ce n’est pas null le cas et n’est pas une chaîne de longueur nulle, elle est utilisée pour construire l’élément ContentType associé à cette pièce jointe.

S’applique à

Attachment(Stream, String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initialise une nouvelle instance de la Attachment classe avec le flux, le nom et les informations de type MIME spécifiés.

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)

Paramètres

contentStream
Stream

Accessible en lecture Stream qui contient le contenu de cette pièce jointe.

name
String

Valeur String qui contient la valeur de la Name propriété associée ContentType à cette pièce jointe. Cette valeur peut être null.

mediaType
String

Qui String contient les informations d’en-tête de contenu MIME pour cette pièce jointe. Cette valeur peut être null.

Exceptions

contentStream a la valeur null.

mediaType n’est pas au format correct.

Exemples

L’exemple de code suivant montre comment appeler ce constructeur.

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

Remarques

S’il mediaType n’est pas null ou égal à String.Empty (« »), il est utilisé pour construire la ContentType classe associée à cette pièce jointe.

Si mediaType et name les deux contiennent des Name informations, la valeur spécifiée est name utilisée. La TransferEncoding propriété est définie sur Base64.

Si la propriété du CanSeek flux est false, la pièce jointe et celle MailMessage qui le contient ne sont pas réutilisables. Vous devez fournir un flux qui peut être recherché pour réutiliser une pièce jointe.

S’applique à