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?
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");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With