Attachment Constructors
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Initialiseert een nieuw exemplaar van de Attachment klasse.
Overloads
| Name | Description |
|---|---|
| Attachment(String) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks. |
| Attachment(Stream, ContentType) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven stream en het opgegeven inhoudstype. |
| Attachment(Stream, String) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven stream en naam. |
| Attachment(String, ContentType) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks en ContentType. |
| Attachment(String, String) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks en MIME-typegegevens. |
| Attachment(Stream, String, String) |
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven gegevensstroom, naam en MIME-type. |
Attachment(String)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks.
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)
Parameters
Uitzonderingen
fileName is null.
fileName is leeg.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u deze constructor aanroept.
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();
}
Opmerkingen
De eigenschappen worden als volgt ingesteld:
| Property | Value |
|---|---|
| MediaType | Plain. |
| TransferEncoding | QuotedPrintable. |
Van toepassing op
Attachment(Stream, ContentType)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven stream en het opgegeven inhoudstype.
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)
Parameters
- contentType
- ContentType
Een ContentType die de gegevens in contentStreambeschrijft.
Uitzonderingen
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u deze constructor aanroept.
// 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();
}
Opmerkingen
De eigenschap TransferEncoding is ingesteld op Base64.
Als de eigenschap van CanSeek de stroom is false, zijn de bijlage en de MailMessage bijlage die deze bevat, niet herbruikbaar. U moet een stroom opgeven die kan worden doorzocht om een bijlage opnieuw te kunnen gebruiken.
Van toepassing op
Attachment(Stream, String)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven stream en naam.
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)
Parameters
- name
- String
Een String met de waarde voor de eigenschap van de NameContentType gekoppelde bijlage. Deze waarde kan zijn null.
Uitzonderingen
contentStream is null.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u deze constructor aanroept.
// 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;
}
Opmerkingen
Als name dit niet null of gelijk is aan String.Empty (""), wordt de ContentType voor deze bijlage samengesteld met de Name eigenschap ingesteld op name. De eigenschap TransferEncoding is ingesteld op Base64.
Als de eigenschap van CanSeek de stroom is false, zijn de bijlage en de MailMessage bijlage die deze bevat, niet herbruikbaar. U moet een stroom opgeven die kan worden doorzocht om een bijlage opnieuw te kunnen gebruiken.
Van toepassing op
Attachment(String, ContentType)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks en 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)
Parameters
- contentType
- ContentType
Een ContentType die de gegevens in fileNamebeschrijft.
Uitzonderingen
fileName is null.
contentType heeft niet de juiste indeling.
Van toepassing op
Attachment(String, String)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven inhoudsreeks en MIME-typegegevens.
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)
Parameters
- mediaType
- String
Een String met de MIME-inhoudsheadergegevens voor deze bijlage. Deze waarde kan zijn null.
Uitzonderingen
fileName is null.
mediaType heeft niet de juiste indeling.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u deze constructor aanroept.
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();
}
Opmerkingen
Als mediaType dit is of gelijk is null aan String.Empty (""), wordt de MediaType eigenschap voor deze bijlage ingesteld op Plain. Als mediaType dit niet null het enige is en geen tekenreeks met lengte nul is, wordt deze gebruikt om de ContentType gekoppelde bijlage samen te stellen.
Van toepassing op
Attachment(Stream, String, String)
Initialiseert een nieuw exemplaar van de Attachment klasse met de opgegeven gegevensstroom, naam en MIME-type.
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)
Parameters
- name
- String
Een String met de waarde voor de eigenschap van de NameContentType gekoppelde bijlage. Deze waarde kan zijn null.
- mediaType
- String
Een String met de MIME-inhoudsheadergegevens voor deze bijlage. Deze waarde kan zijn null.
Uitzonderingen
contentStream is null.
mediaType heeft niet de juiste indeling.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u deze constructor aanroept.
// 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();
}
Opmerkingen
Als mediaType dit niet null of gelijk is aan String.Empty (""), wordt deze gebruikt om de ContentType klasse te maken die aan deze bijlage is gekoppeld.
Als mediaType en name beide informatie bevatten Name , wordt de opgegeven name waarde gebruikt. De eigenschap TransferEncoding is ingesteld op Base64.
Als de eigenschap van CanSeek de stroom is false, zijn de bijlage en de MailMessage bijlage die deze bevat, niet herbruikbaar. U moet een stroom opgeven die kan worden doorzocht om een bijlage opnieuw te kunnen gebruiken.