Sending email using SMTP

Ray Turner
Ray Turner used Ask the Experts™
on
How do you send an email using SMTP, Visual Basic and Visual Studio 2019?  I can't get it to work!!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
What are your implementation details?

-saige-
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Sending is fairly straight forward. Normally you'll use an SMTP library for this, rather than writing your own SMTP code.

As it_saige mentioned, we'll have to see your code to provide assistance.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Aside: There's a big difference between sending email + having email delivery.

You can't just setup SMTP, send an email, imagine it will every arrive at a recipient.

For high deliverability email, you'll use a relay service like https://MailGun.com or setup your own sending infrastructure.

https://www.experts-exchange.com/questions/29158183/Backloading-emails-into-gmail.html covers a minimal checklist of how to get started, if you require building a high deliverability, SMTP infrastructure.

Likely best to start with https://MailGun.com, then move on to setting up your own infrastructure.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
What have you tried? System.Net.Mail?

Your issues might come from your relay. You need to use one (Gmail, O365, ...). What are you using?

Have a look at https://www.emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Ray,

As other experts have suggested, without looking at your code it is next to impossible to guess what is the real culprit.
Also, just so you know, Microsoft's current recommendation is to use library like MailKit
https://github.com/jstedfast/MailKit
per, https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8

Currently, SmtpClient is supported only to provide platform compatibility and should be avoided, if possible.
https://github.com/dotnet/platform-compat/blob/master/docs/DE0005.md

Let me know if you need more information on the same.

Regards,
Chinmay.
Ray TurnerSenior Consultant

Author

Commented:
Chinmay,

How do you use MailKit?  Is it free?
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Ray,

Yes it is free - (MIT License - Check the GitHub Link and search for "License Information
") and for usage, you can check the link I shared: https://github.com/jstedfast/MailKit

For your reference, here is the code, please note that here, SmtpClient is not from System.Net.Mail namespace.
using System;

using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

namespace TestClient {
	class Program
	{
		public static void Main (string[] args)
		{
			var message = new MimeMessage ();
			message.From.Add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"));
			message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
			message.Subject = "How you doin'?";

			message.Body = new TextPart ("plain") {
				Text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"
			};

			using (var client = new SmtpClient ()) {
				// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
				client.ServerCertificateValidationCallback = (s,c,h,e) => true;

				client.Connect ("smtp.friends.com", 587, false);

				// Note: only needed if the SMTP server requires authentication
				client.Authenticate ("joey", "password");

				client.Send (message);
				client.Disconnect (true);
			}
		}
	}
}

Open in new window


If you are asking from code architecture side, most of my project are nowadays based on ASP.Net Core MVC, so I make it a part of my Infrastructure code and simply provide an interface to send out emails using MailKit (so tomorrow if I want to use some other library I can safely replace MailKit, for some of our customers, we use SendGrid - and I just replace MailKit with SendGrid API).

If you can share more details on what is the business + technical requirements, I might be able to provide more information.

Regards,
Chinmay.
Ray TurnerSenior Consultant

Author

Commented:
Thanks!  Microsoft move away from the email business some time ago (https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8) and it's been costly to find solutions.  I'm currently paying an annual fee for a solution and I was curious to find a free solution.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
If you opt for using MailKit or any other MTA-esque code which sends email from IPs you own, you must still go through all the IP setup checklist items for every sending IP you use.

https://www.experts-exchange.com/questions/29158183/Backloading-emails-into-gmail.html

If you expect to have any level of deliverability.

Remember: Sending email is easy. Having email deliver is a monumentally complex task.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Ray,

With advent of .Net Core, I think almost all products will have a free, open source, community driven (But Microsoft backed/overwatched) alternative.

Let me know if there are other similar challenges you are facing, I might be able to drop in some pointers.

Regards,
Chinmay.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial