Sample Code to Send Email Progmatically on ASP.NET Hosting

If you are using .NET hosting, you can send email inside your code using following sample code. Please make sure you create below email address using your email control panel.

 static void Main(string[] args)
        {
            string Username = "your email address - created from email control panel";
            string Password = "email password";
            string toEmail = "your email @gmail.com"; // Receiptant email address
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mx.lankahost.net";  // Email Host Name
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(Username, Password);

            using (var message = new MailMessage(Username, toEmail))
            {
                message.Subject = "Test";
                message.Body = "Test Message Goes Here";
                message.IsBodyHtml = true;
                smtp.Send(message);
            }
        }

  • 2 Users Found This Useful
Was this answer helpful?