How to send email with attachment in C# via Office365.com ?

This aspx code sends email with attachment

 

<%@ Page Language="C#" AutoEventWireup="true" debug = "true" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Net.Mime" %>
<%@ Import namespace="System.Net.Mail" %>
<%@ Import namespace="System.Net.Security" %>
<%@ Import namespace="System.Security.Cryptography.X509Certificates" %>


<%

string SMTPLogin = "login@yourdomain.com";
string SMTPLoginPass = "loginpassword";
string SMTPServer = "smtp.office365.com";
string SMTPPort = "587";
string SenderEmail = "sender@yourdomain.com";

string sToEmails = "to@someone.com";
string sSubject = "test subject";
string sCCEmails = "cc@someone.com";
string sMsg = "";
string tplName = "test";

int port;
port = Convert.ToInt32(SMTPPort);

try
{
      NetworkCredential myEmail = new NetworkCredential(SMTPLogin,SMTPLoginPass);
       SmtpClient mailClient = new SmtpClient(SMTPServer, port);
       MailMessage mailMsg = new MailMessage(); 

       mailClient.EnableSsl = true;
       mailClient.UseDefaultCredentials = false;
       mailClient.Credentials = myEmail;
       mailMsg.IsBodyHtml = true;
       mailMsg.From = new MailAddress(SenderEmail);

       string sAppPath = HttpContext.Current.Server.MapPath("~");

       ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

        sMsg = "Test emai with attachment";

        // add attachment
        string PathToAttachment = sAppPath + "/App_Data/test.pdf";

         System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(PathToAttachment);
          attachment.Name = "test.pdf"; // set name here
          mailMsg.Attachments.Add(attachment);

     }


     mailMsg.To.Add(new MailAddress(sToEmails));

    mailMsg.CC.Add ( new MailAddress(sCCEmails));

    mailMsg.Subject = sSubject;
    mailMsg.Body = sMsg;
    mailClient.Send(mailMsg);

    Response.Write("message send successfully !");

}
catch (SmtpFailedRecipientException ex) 
{
       SmtpStatusCode statusCode = ex.StatusCode;

       if (statusCode == SmtpStatusCode.MailboxBusy ||
            statusCode == SmtpStatusCode.MailboxUnavailable ||
            statusCode == SmtpStatusCode.TransactionFailed)
      { 
            // Display message like 'Mail box is busy', 'Mailbox is unavailable' or 'Transaction is failed'
            Response.Write("SMTP Recipient Exception :" + ex.Message + SMTPServer + ex.StackTrace);
      }
      else
     { 
           Response.Write("SMTP Recipient Exception 2 :" + ex.Message + SMTPServer + ex.StackTrace);
      }
}
catch (System.Net.Mail.SmtpException ex)
{
          Response.Write("SMTP Exception :" + ex.Message + SMTPServer + ex.StackTrace);
}
catch (Exception ex)
{
           Response.Write(ex.Message + SMTPServer + ex.StackTrace);
}
%>