Emails can be sent programmatically using System.Net.Mail.SmtpClient.
Follow the below code –
Use System.Net.Mail namespace to your code file.
const string subject = “Test Mail”;
const string body = “Sent using SMTPClient”;
var smtp = new SmtpClient
Host = “smtp.gmail.com”,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(“[email protected]”, “your password”),
using (var message = new MailMessage”from address”,”to address”)
Subject = subject,
Body = body
Following information is required for setting up SMTP client-
- The SMTP host and port that you are using to send email.
- Credentials of the account you are using to send emails.
- The e-mail address of the sender and the recipients.
- The message subject and body.
I have used Gmail as the host in my example above. You can use any other email host providers as well for sending emails provided you have the host address and port number information with you.
There are several optional properties as well which you can make use of as per your requirements. For example, attachments can also be sent using this class as a part of the emails.
Those using Godaddy Email hosting for sending emails, refer below code-
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.From = new MailAddress(“your address”,”Display Name”);
MyMailMessage.Subject = “Message Sent”;
MyMailMessage.IsBodyHtml = true;
MyMailMessage.Body = “<table><tr><td>” + “Hi “+name+”,<br /> <br />Thanks for the message.<br />Will get back to you on that soon.<br /><br /></td></tr></table>”;
SmtpClient SMTPServer = new SmtpClient(“smtpout.secureserver.net”);
SMTPServer.Credentials = new System.Net.NetworkCredential(“your email address on gadaddy”, “password”);
Please refer the below MSDN link for more details on SMTPClient class and available properties and methods:
Please leave your comments and feedback.
Thanks for checking out !