How to send emails in .NET part 2: the MailAddress object
August 20, 2014 1 Comment
In the previous post on this topic we saw how to send a plain text email with the MailMessage and SmtpClient objects.
You can refine the From and To fields of the message using the MailAddress object. You can specify the email address, a display name and an encoding. Specifying an encoding is seldom necessary.
So if you’d like to joke with your colleagues then this is one option:
MailAddress from = new MailAddress("andras.nemes@company.com", "Your boss");
MailAddress to = new MailAddress("john.smith@company.com");
string subject = "You are fired.";
string plainTextBody = "See you in hell.";
MailMessage mailMessage = new MailMessage(from, to);
mailMessage.Subject = subject;
mailMessage.Body = plainTextBody;
string smtpServer = "mail.company.com";
SmtpClient client = new SmtpClient(smtpServer);
client.Send(mailMessage);
The recipient will see an email similar to the following:
Of course they will eventually see the actual email address of the sender but they might get scared at first.
Read all posts related to emailing in .NET here.
Hi,
you can also fake the sender e-mail-address 🙂