Task

Write a function to send an email.

The function should have parameters for setting From, To and Cc addresses; the Subject, and the message text, and optionally fields for the server name and login details.

 If appropriate, explain what notifications of problems/success are given.
 Solutions using libraries or functions from the language are preferred, but failing that, external programs can be used with an explanation.
 Note how portable the solution given is between operating systems when multi-OS languages are used.


(Remember to obfuscate any sensitive data used in examples)




 
static void Main(string[] args)
{
    //First of all construct the SMTP client
 
    SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587); //I have provided the URI and port for GMail, replace with your providers SMTP details
    SMTP.EnableSsl = true; //Required for gmail, may not for your provider, if your provider does not require it then use false.
    SMTP.DeliveryMethod = SmtpDeliveryMethod.Network;
    SMTP.Credentials = new NetworkCredential("YourUserName", "YourPassword");
    MailMessage Mail = new MailMessage("yourEmail@address.com", "theirEmail@address.com");
 
 
    //Then we construct the message
 
    Mail.Subject = "Important Message";
    Mail.Body = "Hello over there"; //The body contains the string for your email
    //using "Mail.IsBodyHtml = true;" you can put an HTML page in your message body
 
    //Then we use the SMTP client to send the message
 
    SMTP.Send(Mail);
 
    Console.WriteLine("Message Sent");
}
 