Below you can find fully working implementation of the method, which you can use to send mails.
Simply change the property values i.e. “user@domain.tld” to the email address you want to send from, provide your password, and define the recipient (string to).
You may notice, that other code samples doesn’t include the password field, but it’s absolutely necessary to send email through the safe connection.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net; namespace Email { class Program { static void Main(string[] args) { string SMTPhost = "mail.web-solutions.dk"; int SMTPport = 465; string from = "user@domain.tld"; string password = "password"; string to = "user2@domain.tld"; string subject = ""; string body = @""; MailMessage message = new MailMessage(from, to, subject, body); SmtpClient client = new SmtpClient(SMTPhost, SMTPport); client.Credentials = new System.Net.NetworkCredential(from, password); try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught: {0}", ex.ToString()); } } } }
Back Last updated: May 26, 2015 by