Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending E-Mail in C#

Tags:

c#

email

.net-3.5

I’m using .NET 3.5, and I want to automatically send a mail. I’m currently using the following:

Microsoft.Office.Interop.Outlook.MailItem mailMsg = 
    (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem(
     Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailMsg.To = recipient;
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.Send();

However, I’ve found several articles that seem to imply I should be using the following method:

System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
mailmsg.To = recipient;
mailmsg.Subject = subject;
mailmsg.Body = body;

Can anyone tell me what the difference between the two namespaces if, and why you might want to use one over the other?

like image 263
Paul Michaels Avatar asked May 28 '10 11:05

Paul Michaels


People also ask

How to send email in C# console application?

Sending emails from C# using an SMTP server requires only a few lines of code: var smtpClient = new SmtpClient("smtp.gmail.com") { Port = 587, Credentials = new NetworkCredential("username", "password"), EnableSsl = true, }; smtpClient. Send("email", "recipient", "subject", "body");

How do I send an email using MailKit?

C# send simple mail with Mailkit Net. Smtp; using MailKit. Security; using MimeKit; var host = "smtp.mailtrap.io"; var port = 2525; var username = "username"; // get from Mailtrap var password = "password"; // get from Mailtrap var message = new MimeMessage(); message.


1 Answers

The first one, I assume, required Outlook to be installed in the machine so that the Office Interop assemblies are installed. The second one is pure .Net framework.

like image 99
Colin Desmond Avatar answered Oct 06 '22 02:10

Colin Desmond