10 Sep

C# Send an Email with Attachment from HotMail

This example shows how to send an email with attachment from HotMail.
To send an email using HotMail, we need to add a reference to the dynamic link library System.Net.Mail.
To do this:

  • Go to solution explorer of your project
  • Select add a reference
  • Click on .Net Tab
  • Select System.Net.Mail.

The HotMail SMTP Server requires an encrypted connection (SSL) on port 25.
Sample :

        public void SendMailUsingHotmail()
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("sender@hotmail.com");
                
                //receiver email adress
                mailMessage.To.Add("receiver@gmail.com");
                
                //subject of the email
                mailMessage.Subject = "This is a subject";
                
                //attach the file
                mailMessage.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
                mailMessage.Body = "Body of the email";
                mailMessage.IsBodyHtml = true;
                
                //SMTP client
                SmtpClient smtpClient = new SmtpClient("smtp.live.com");
                //port number for Hot mail
                smtpClient.Port = 25;
                //credentials to login in to hotmail account
                smtpClient.Credentials = new NetworkCredential("sender@hotmail.com", "password");
                //enabled SSL
                smtpClient.EnableSsl = true;
                //Send an email
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {

            }
        }