C send email with attachment system.net.mail

Here is an example of how to send an email with an attachment using the System.Net.Mail namespace in C#:

using System;
using System.Net.Mail;
using System.Net.Mime;

// Create a new MailMessage object
MailMessage mail = new MailMessage();

// Set the sender and recipient
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");

// Set the subject and body of the email
mail.Subject = "Test email with attachment";
mail.Body = "This is a test email with an attachment";

// Create a new Attachment object
Attachment attachment = new Attachment("path/to/attachment.txt", "text/plain");

// Add the attachment to the email
mail.Attachments.Add(attachment);

// Create a new SmtpClient object
SmtpClient client = new SmtpClient();

// Set the SMTP server and port
client.Host = "smtp.example.com";
client.Port = 587;

// Set the username and password for the SMTP server
client.Credentials = new NetworkCredential("username", "password");

// Send the email
client.Send(mail);

This code creates a new MailMessage object and sets the sender, recipient, subject, and body of the email. It then creates a new Attachment object and adds it to the email using the Attachments.Add method. Finally, it creates a new SmtpClient object and sets the SMTP server, port, username, and password. The email is then sent using the Send method.

You can also use the MailMessage object to set the attachment's content type, disposition, and filename:

attachment.ContentDisposition.FileName = "attachment.txt";
attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
attachment.ContentDisposition.FileName = "attachment.txt";

You can also use the MailMessage object to set the attachment's encoding and charset:

attachment.ContentEncoding = System.Text.Encoding.UTF8;
attachment.ContentType.CharSet = "utf-8";

Make sure to replace the placeholders ([email protected], [email protected], path/to/attachment.txt, smtp.example.com, username, and password) with your actual values.

Also, you can use MailMessage object to set the email's priority, importance, and other properties:

mail.Priority = MailPriority.High;
mail.Importance = MailImportance.High;

You can also use MailMessage object to set the email's reply-to address and other properties:

mail.ReplyTo = new MailAddress("[email protected]");

Please note that this is just an example, you should adjust the code to fit your specific needs and requirements.