Software Architecture-C#-Send email via Google smtp server with attachment

Following code send email via Google smtp server with attachment.

           ///Basic Email message 
            MailMessage mailMessage = new MailMessage();
            // Email to send to
            MailAddress toEmail = new MailAddress("shahzadsb@yahoo.com", "Shahzad Sarwar");
            mailMessage.To.Add(toEmail);            
            // set subject
            mailMessage.Subject = "My Email Subject.....";            
            // set body
            mailMessage.Body = "My Email Body..................";
            mailMessage.IsBodyHtml = true;
            // attachment
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment("G:\\test.txt");
            mailMessage.Attachments.Add(attachment);
            // set from email
            mailMessage.From = new MailAddress("shahzadsb78@gmail.com", "Shahzad Sarwar");
            // Identify the credentials to login to the gmail account  
            string sendEmailsFrom = "shahzadsb78@gmail.com";            
            string sendEmailsFromPassword = "888888";
            NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
            SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
            mailClient.EnableSsl = true;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.UseDefaultCredentials = false;
            mailClient.Timeout = 20000;
            mailClient.Credentials = cred;
            mailClient.Send(mailMessage);
Advertisements

One Response to Software Architecture-C#-Send email via Google smtp server with attachment

  1. Ssmm987 says:

    Thnx, definitely gonna use this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: