System.net.mail.mailmessage
A popular topic!
System.Net.Mail.MailMessage
is a.NET class used to create and send email messages using the Simple Mail Transfer Protocol (SMTP). It is part of the.NET Framework and is commonly used in ASP.NET and Windows Forms applications.
Here are some key properties and methods of the MailMessage
class:
Properties:
Body
: The body of the email message as a string.BodyEncoding
: The encoding used for the body of the email message.CC
: A collection of email addresses to include in the carbon copy (CC) field.From
: The email address of the sender.IsBodyHtml
: A boolean indicating whether the body of the email message is in HTML format.Priority
: The priority of the email message (High, Normal, or Low).Subject
: The subject of the email message.To
: A collection of email addresses to include in the recipient list.
Methods:
Send()
: Sends the email message using the specified SMTP server.SendAsync()
: Sends the email message asynchronously using the specified SMTP server.Save()
: Saves the email message to a file.
Example usage:
Here's an example of how to create and send an email message using the MailMessage
class:
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("[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.";
// Set the priority of the email message
mail.Priority = MailPriority.Normal;
// Create a new SmtpClient object
SmtpClient client = new SmtpClient();
// Set the SMTP server's address and port
client.Host = "smtp.example.com";
client.Port = 587;
// Send the email message
client.Send(mail);
Note that this is just a basic example, and you may need to add additional configuration and error handling depending on your specific requirements.