Attachment Konstruktorer
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Initierar en ny instans av Attachment klassen.
Överlagringar
| Name | Description |
|---|---|
| Attachment(String) |
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen. |
| Attachment(Stream, ContentType) |
Initierar en ny instans av Attachment klassen med den angivna ström- och innehållstypen. |
| Attachment(Stream, String) |
Initierar en ny instans av Attachment klassen med den angivna strömmen och namnet. |
| Attachment(String, ContentType) |
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen och ContentType. |
| Attachment(String, String) |
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen och MIME-typinformationen. |
| Attachment(Stream, String, String) |
Initierar en ny instans av Attachment klassen med den angivna dataströmmen, namnet och MIME-typinformationen. |
Attachment(String)
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen.
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)
Parametrar
- fileName
- String
En String som innehåller en filsökväg som ska användas för att skapa den här bifogade filen.
Undantag
fileName är null.
fileName är tom.
Exempel
Följande kodexempel visar hur du anropar den här konstruktorn.
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();
}
Kommentarer
Egenskaperna anges på följande sätt:
| Property | Value |
|---|---|
| MediaType | Plain. |
| TransferEncoding | QuotedPrintable. |
Gäller för
Attachment(Stream, ContentType)
Initierar en ny instans av Attachment klassen med den angivna ström- och innehållstypen.
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)
Parametrar
- contentType
- ContentType
En ContentType som beskriver data i contentStream.
Undantag
Exempel
Följande kodexempel visar hur du anropar den här konstruktorn.
// 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();
}
Kommentarer
Egenskapen TransferEncoding är inställd på Base64.
Om strömmens CanSeek egenskap är kan falseden bifogade filen och den som innehåller den MailMessage inte återanvändas. Du måste ange en dataström som kan sökas efter för att återanvända en bifogad fil.
Gäller för
Attachment(Stream, String)
Initierar en ny instans av Attachment klassen med den angivna strömmen och namnet.
public:
Attachment(System::IO::Stream ^ contentStream, System::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)
Parametrar
- name
- String
En String som innehåller värdet för egenskapen för Name den som är associerad med den ContentType här bifogade filen. Det här värdet kan vara null.
Undantag
contentStream är null.
Exempel
Följande kodexempel visar hur du anropar den här konstruktorn.
// 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;
}
Kommentarer
Om name inte är eller lika String.Empty med (") skapas för den ContentType här bifogade filen med egenskapen inställd på Namename.null Egenskapen TransferEncoding är inställd på Base64.
Om strömmens CanSeek egenskap är kan falseden bifogade filen och den som innehåller den MailMessage inte återanvändas. Du måste ange en ström som kan sökas igenom för att kunna återanvända en bifogad fil.
Gäller för
Attachment(String, ContentType)
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen och 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)
Parametrar
- fileName
- String
En String som innehåller en filsökväg som ska användas för att skapa den här bifogade filen.
- contentType
- ContentType
En ContentType som beskriver data i fileName.
Undantag
fileName är null.
contentType är inte i rätt format.
Gäller för
Attachment(String, String)
Initierar en ny instans av Attachment klassen med den angivna innehållssträngen och MIME-typinformationen.
public:
Attachment(System::String ^ fileName, System::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)
Parametrar
- mediaType
- String
En String som innehåller information om MIME Content-Header för den här bifogade filen. Det här värdet kan vara null.
Undantag
fileName är null.
mediaType är inte i rätt format.
Exempel
Följande kodexempel visar hur du anropar den här konstruktorn.
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();
}
Kommentarer
Om mediaType är null eller lika med String.Empty (") är egenskapen för den MediaType här bifogade filen inställd på Plain. Om mediaType inte null är och inte är en sträng med noll längd används den för att konstruera den associerade med den ContentType här bifogade filen.
Gäller för
Attachment(Stream, String, String)
Initierar en ny instans av Attachment klassen med den angivna dataströmmen, namnet och MIME-typinformationen.
public:
Attachment(System::IO::Stream ^ contentStream, System::String ^ name, System::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)
Parametrar
- name
- String
En String som innehåller värdet för egenskapen för Name den som är associerad med den ContentType här bifogade filen. Det här värdet kan vara null.
- mediaType
- String
En String som innehåller information om MIME Content-Header för den här bifogade filen. Det här värdet kan vara null.
Undantag
contentStream är null.
mediaType är inte i rätt format.
Exempel
Följande kodexempel visar hur du anropar den här konstruktorn.
// 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();
}
Kommentarer
Om mediaType inte null är eller lika String.Empty med (""), används den för att konstruera klassen som är associerad med den ContentType här bifogade filen.
Om mediaType och name båda innehåller Name information används värdet som anges i name . Egenskapen TransferEncoding är inställd på Base64.
Om strömmens CanSeek egenskap är kan falseden bifogade filen och den som innehåller den MailMessage inte återanvändas. Du måste ange en ström som kan sökas igenom för att kunna återanvända en bifogad fil.