System.net.mail
System.Net.Mail
is a namespace in the.NET Framework that provides classes for sending and receiving email messages. It is part of the.NET Framework Class Library and is used to send and receive email messages using the Simple Mail Transfer Protocol (SMTP).
Here are some of the key classes and methods in the System.Net.Mail
namespace:
Sending Email
MailMessage
: Represents an email message that can be sent using theSmtpClient
class.SmtpClient
: Sends email messages using the SMTP protocol.MailAddress
: Represents an email address.MailAttachment
: Represents an attachment to an email message.
Receiving Email
Pop3Client
: Retrieves email messages from a POP3 server.ImapClient
: Retrieves email messages from an IMAP server.
Email Message Properties
Subject
: Gets or sets the subject of the email message.Body
: Gets or sets the body of the email message.From
: Gets or sets the sender's email address.To
: Gets or sets the recipient's email address.CC
: Gets or sets the carbon copy recipients' email addresses.Bcc
: Gets or sets the blind carbon copy recipients' email addresses.
Email Attachment Properties
Attachment
: Gets or sets the attachment file name.ContentDisposition
: Gets or sets the content disposition of the attachment.ContentType
: Gets or sets the content type of the attachment.
Here is an example of how to use the System.Net.Mail
namespace to send an email message:
using System.Net.Mail;
// Create a new MailMessage object
MailMessage mail = new MailMessage();
// Set the sender's email address
mail.From = new MailAddress("[email protected]");
// Set the recipient's email address
mail.To.Add(new MailAddress("[email protected]"));
// Set the subject of the email message
mail.Subject = "Test Email";
// Set the body of the email message
mail.Body = "This is a test email message.";
// Create a new SmtpClient object
SmtpClient client = new SmtpClient();
// Set the SMTP server's address
client.Host = "smtp.example.com";
// Set the SMTP server's port
client.Port = 25;
// Send the email message
client.Send(mail);
Note that the System.Net.Mail
namespace is part of the.NET Framework and is not available in.NET Core. In.NET Core, you can use the System.Net.Mail.SmtpClient
class from the System.Net.Mail
namespace, but you will need to use a third-party library such as MailKit
or NReco.Mail
to send and receive email messages.