Attachment Konstruktoren
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Initialisiert eine neue Instanz der Attachment-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| Attachment(String) |
Initialisiert eine neue Instanz der Attachment Klasse mit der angegebenen Inhaltszeichenfolge. |
| Attachment(Stream, ContentType) |
Initialisiert eine neue Instanz der Attachment Klasse mit dem angegebenen Datenstrom- und Inhaltstyp. |
| Attachment(Stream, String) |
Initialisiert eine neue Instanz der Attachment Klasse mit dem angegebenen Datenstrom und Namen. |
| Attachment(String, ContentType) |
Initialisiert eine neue Instanz der Attachment Klasse mit der angegebenen Inhaltszeichenfolge und ContentType. |
| Attachment(String, String) |
Initialisiert eine neue Instanz der Attachment Klasse mit den angegebenen Inhaltszeichenfolgen- und MIME-Typinformationen. |
| Attachment(Stream, String, String) |
Initialisiert eine neue Instanz der Attachment Klasse mit den angegebenen Datenstrom-, Namens- und MIME-Typinformationen. |
Attachment(String)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit der angegebenen Inhaltszeichenfolge.
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)
Parameter
Ausnahmen
fileName ist null.
fileName ist leer.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie dieser Konstruktor aufgerufen wird.
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();
}
Hinweise
Die Eigenschaften werden wie folgt festgelegt:
| Eigentum | Wert |
|---|---|
| MediaType | Plain. |
| TransferEncoding | QuotedPrintable. |
Gilt für:
Attachment(Stream, ContentType)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit dem angegebenen Datenstrom- und Inhaltstyp.
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)
Parameter
- contentType
- ContentType
Eine ContentType , die die Daten in contentStreambeschreibt.
Ausnahmen
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie dieser Konstruktor aufgerufen wird.
// 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();
}
Hinweise
Die TransferEncoding-Eigenschaft ist auf Base64 gesetzt.
Wenn die Eigenschaft des Datenstroms CanSeek lautet false, sind die Anlage und die MailMessage darin enthaltene Anlage nicht wiederverwendbar. Sie müssen einen Datenstrom angeben, der durchsucht werden kann, um eine Anlage wiederzuverwenden.
Gilt für:
Attachment(Stream, String)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit dem angegebenen Datenstrom und Namen.
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)
Parameter
- name
- String
A String , das den Wert für die Name Eigenschaft der ContentType zugeordneten Anlage enthält. Dieser Wert kann null sein.
Ausnahmen
contentStream ist null.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie dieser Konstruktor aufgerufen wird.
// 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;
}
Hinweise
Ist name dies nicht null oder gleich String.Empty (""), wird die ContentType Für diese Anlage mit der Eigenschaft erstellt, auf namedie Name sie festgelegt ist. Die TransferEncoding-Eigenschaft ist auf Base64 gesetzt.
Wenn die Eigenschaft des Datenstroms CanSeek lautet false, sind die Anlage und die MailMessage darin enthaltene Anlage nicht wiederverwendbar. Sie müssen einen Datenstrom angeben, der durchsucht werden kann, um eine Anlage wiederzuverwenden.
Gilt für:
Attachment(String, ContentType)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit der angegebenen Inhaltszeichenfolge und 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)
Parameter
- contentType
- ContentType
Eine ContentType , die die Daten in fileNamebeschreibt.
Ausnahmen
fileName ist null.
contentType ist nicht im richtigen Format vorhanden.
Gilt für:
Attachment(String, String)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit den angegebenen Inhaltszeichenfolgen- und MIME-Typinformationen.
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)
Parameter
- mediaType
- String
A String that contains the MIME Content-Header information for this attachment. Dieser Wert kann null sein.
Ausnahmen
fileName ist null.
mediaType ist nicht im richtigen Format vorhanden.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie dieser Konstruktor aufgerufen wird.
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();
}
Hinweise
Wenn mediaType gleich nullString.Empty (""), wird die MediaType Eigenschaft für diese Anlage auf .Plain Wenn mediaType dies nicht null der Fall ist und keine leere Zeichenfolge ist, wird sie verwendet, um die ContentType zugeordnete Anlage zu erstellen.
Gilt für:
Attachment(Stream, String, String)
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
- Quelle:
- Attachment.cs
Initialisiert eine neue Instanz der Attachment Klasse mit den angegebenen Datenstrom-, Namens- und MIME-Typinformationen.
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)
Parameter
- name
- String
A String , das den Wert für die Name Eigenschaft der ContentType zugeordneten Anlage enthält. Dieser Wert kann null sein.
- mediaType
- String
A String that contains the MIME Content-Header information for this attachment. Dieser Wert kann null sein.
Ausnahmen
contentStream ist null.
mediaType ist nicht im richtigen Format vorhanden.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie dieser Konstruktor aufgerufen wird.
// 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();
}
Hinweise
Ist mediaType dies nicht null oder gleich String.Empty (""), wird sie verwendet, um die ContentType dieser Anlage zugeordnete Klasse zu erstellen.
Wenn mediaType und name beide Informationen enthalten Name , wird der angegebene name Wert verwendet. Die TransferEncoding-Eigenschaft ist auf Base64 gesetzt.
Wenn die Eigenschaft des Datenstroms CanSeek lautet false, sind die Anlage und die MailMessage darin enthaltene Anlage nicht wiederverwendbar. Sie müssen einen Datenstrom angeben, der durchsucht werden kann, um eine Anlage wiederzuverwenden.