Sending Email With Smtp C#

What is SMTP?

SMTP, which consists of the initials of the words Simple Mail Transfer Protocol, is "Simple mail transfer (Sending) protocol". Smpt, also known as the outgoing mail server, is used to send mail. Pop3 and Imap protocols are used as incoming mail servers. SMTP mail server is useful when sending e-mails with 3rd party software.

 

What are the settings that need to be made in 3rd party mail programs?

It is necessary to install a 3rd party mail program. Outgoing and incoming mail server settings must be made and the SMTP port must be entered.

 

What is SMTP port?

SMTP port is a port through which we send e-mail when sending e-mail. By default it is port 25. However, due to the increase in spam e-mail sending incidents, port 25 was closed and the SMTP port number was changed to port 587. You should set this port as port 587 in your 3-party mail program.

 

What happens if I don't set the SMTP port number?

If you do not set the SMTP port number, you cannot send e-mails.

 

How to send an e-mail using a 3rd party e-mail program?

When you try to send an e-mail with a 3rd party e-mail program, a connection is first tried to be established through the account and port information you entered. Therefore, you must enter your account information and SMTP port information correctly. If we have set everything up correctly, when we write an e-mail and send it, a connection is made to our opposite SMTP server via port 587. Your authentication is done through this port. If the verification is successful, the e-mail you want to send will be sent.

 

What is the difference between Smtp port 25 and Smtp port 587?

Although both stmp ports perform the same operation, the main difference between them is this. While authentication is not mandatory on port 25, authentication is mandatory on port 587, thus trying to prevent spam e-mails.

 

So how do I send e-mail using .NET Smtp?

Come on, let's look at this together. First of all, we need to add the System.Net.Mail reference to use Smtp. We must also add the System.Text reference to set the encoding setting for UTF8 (Encoding.UTF8). Then we need to set the Port, Host, EnableSsl (i.e. secure access), Timeout, DeliveryMethod and Credentials settings. After making these settings, we can only create the MailMessage object and complete the sending process. If we look at the codes, we can think of a code block like this.

 

SmtpClient client = new SmtpClient();
client.Port = 587; // Generally ports 587 and 25 are used.
client.Host = "mail.tulparyazilim.com"; // Your host's mail domain for SMTP.
client.EnableSsl = false; // Security settings may vary depending on the host and the sent server.
client.Timeout = 10000; // Timeout in milliseconds
client.DeliveryMethod = SmtpDeliveryMethod.Network; // Sending method of the e-mail
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourmailaccount@mail.com", "password"); // Here you need to configure the account you will use to send e-mail.
MailMessage mm = new MailMessage("info@tulparyazilim.com", "info@tulparyazilim.com", "subject", "html content"); // You can configure the e-mail address from which e-mail address to where, subject and content.
mm.IsBodyHtml = true; // True: Send as Html, False: Send as Text
mm.BodyEncoding = UTF8Encoding.UTF8; // UTF8 encoding setting
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; // Give warning when there is an error
client.Send(mm); // Send e-mail

 

Yes, sending e-mail in general is like this in C# and .NET. The example shown is sending an e-mail to only one user. If you want to send multiple emails. You can add code like this or make changes to the code block. 

 

MailMessage msg = new MailMessage();
msg.Body = "Sample Text";
msg.To.Add("example@example.com");
msg.To.Add("example2@example2.com");

 

Additionally, if you want the names of the e-mail owners to appear, you can also make an arrangement like this.

 

MailAddress to = new MailAddress(String.Format("{0} <{1}>", "Display Title", "Email address"); 

 

You can also do the same format for your own email address, so that your title appears when you send an email.

This is how to send e-mail via SMTP in simple terms with C#.

We wish everyone good work.

 

Would you like to have an interview?

We help you with your business's most critical issues and opportunities. How about getting permanent change and results together?