SamKr
11/19/2014 - 3:36 PM

Send an e-mail

Send an e-mail

using System.Net;
using System.Net.Mail;

public static void SendMail(string msg, string subject)
{
    try
    {
        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential = new NetworkCredential("user", "pass");
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress("sender@address");
        MailAddress toAddress1 = new MailAddress("receiver@address");

        smtpClient.Host = "smtpserver";
        smtpClient.Port = 25;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;

        message.From = fromAddress;
        message.Subject = subject;
        message.IsBodyHtml = false;

        message.Body = msg;
        message.To.Add(toAddress1);

        smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

        smtpClient.SendAsync(message, "normal msg");
    }
    catch
    {
        //MessageBox.Show("Error tijdens verzenden notificatie: " + ex.Message);
    }
}

private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    try
    {
        String token = (string)e.UserState;

        if (e.Cancelled)
        {
            //
        }
        if (e.Error != null)
        {
            //
        }
        else
        {
            //
        }
    }
    catch { }
}