Search This Blog

Thursday, March 27, 2014

C# Send email by using gmail smtp With Attachment

Namespace:

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

Method Call: 


Sendmail("Subject", "Body", "fromaddress@XXX.YYY", "toaddress@aaa.ccc", "FilePath");

Method:
        public static void Sendmail(string subject, string body, string from, string to, string newFileName)
        {
            MailAddress mailfrom = new MailAddress(from);
            MailAddress mailto = new MailAddress(to);
            MailMessage msg = new MailMessage(mailfrom, mailto);


            Attachment att = new Attachment(newFileName);
            msg.Attachments.Add(att);
            msg.Subject = subject;
            msg.Body = body;
            var client = new SmtpClient("smtp.gmail.com", 587)
           {
                    Credentials = new NetworkCredential("username@gmail.com", "gmailpassword"),
                    EnableSsl = true
             };

            client.Send(msg);
        }

No comments:

Post a Comment