Saturday, February 09, 2008

Sending mail using SmtpClient

Self explained sample:
// Mail message construction
MailMessage mm = new MailMessage("youraccount@gmail.com", "destination@mail.com");

// content
mm.Subject = "testing message";
mm.Body = "hello... from .net c# mailmessage";
mm.CC.Add("copycc@mail.com");
mm.CC.Add("copycc2@mail.com");
mm.Bcc.Add("copybcc@mail.com");

// some attachments
mm.Attachments.Add(new Attachment("c:\\filename.txt"));

// Sending message
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);

// our account credentials
sc.Credentials = new NetworkCredential("youraccount@gmail.com", "yourpassword", "");
sc.EnableSsl = true;

// Catching result
try
{
    sc.Send(mm);
    MessageBox.Show("Message sent");
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message);
}
For send HTML content and pictures, we need to use Alternated views with LinkedResource instead of the Body property.
// Mail message construction
MailMessage mm = new MailMessage("youraccount@gmail.com", "destination@mail.com");

// content
mm.Subject = "testing message";

//mm.Body = "hello... from .net c# mailmessage"; // deprecated, now we're using alternate views

mm.CC.Add("copycc@mail.com");
mm.CC.Add("copycc2@mail.com");
mm.Bcc.Add("copybcc@mail.com");

// some attachments
mm.Attachments.Add(new Attachment("c:\\filename.txt"));

// An HTML message body including one image
string htmlBody = "<html><body>hello... from .net c# mailmessage<br><img src=\"cid:MyPicture\"></body></html>";

AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);

// Now, linking our image as an embedded resource
LinkedResource MyPicture = new LinkedResource("c:\\MyPicture.jpg", MediaTypeNames.Image.Jpeg);
MyPicture.ContentId = "MyPicture";

avHtml.LinkedResources.Add(MyPicture); // Linking the image

// For clients that do not support HTML content
string textBody = "hello... from .net c# mailmessage... You're missing my picture.";

AlternateView avText = AlternateView.CreateAlternateViewFromString(textBody, null, MediaTypeNames.Text.Plain);

// Add the alternate views
mm.AlternateViews.Add(avHtml);
mm.AlternateViews.Add(avText);


// Sending message
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);

// our account credentials
sc.Credentials = new NetworkCredential("youraccount@gmail.com", "yourpassword", "");
sc.EnableSsl = true;

// Catching result
try
{
    sc.Send(mm);
    MessageBox.Show("Message sent");
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message);
}
For asynchronous operations:
// Mail message construction
MailMessage mm = new MailMessage("youraccount@gmail.com", "destination@mail.com");

// content
mm.Subject = "testing message";
mm.Body = "hello... from .net c# mailmessage";
mm.CC.Add("copycc@mail.com");
mm.CC.Add("copycc2@mail.com");
mm.Bcc.Add("copybcc@mail.com");

// some attachments
mm.Attachments.Add(new Attachment("c:\\filename.txt"));

// Sending message
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);

// our account credentials
sc.Credentials = new NetworkCredential("youraccount@gmail.com", "yourpassword", "");
sc.EnableSsl = true;

// Asynchronous sending
sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted);

sc.SendAsync(mm, null);
void sc_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Cancelled)
        MessageBox.Show("Message cancelled");
    else if (e.Error != null)
        MessageBox.Show("Error: " + e.Error.ToString());
    else
        MessageBox.Show("Message sent");
}

4 comments:

Anonymous said...

Hi
Very Excellent Discription
Thank ypu for ur nice Work
info@powersetshop.ir

Werther said...

Best example on this topic I could find in the Internet.

Greg said...

Great example!! As the post above says, this is the best example I could find on the net. First one I was able to implement and run on first try.

Anonymous said...

You are a champion! This one actually worked in every client I tried it in, which wasn't many... :)


View My Stats