Saturday, May 11, 2013

Sending email with attachments using C# and Gmail

Sending email with .Net is far simpler than it is in Delphi (using Indy).

Put the following in your classe's "using" section:
using System.Net.Mail;
using System.Net;

Include this method in your class

        private bool SendViaGmail()
        {
            try
            {
                var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("myusername@gmail.com", "mypassword"),
                    EnableSsl = true
                };
                var msg = new MailMessage("myusername@gmail.com",
                    "mybuddy@example.com",
                    "Email subject",
                    "This is the email body...");
                msg.Attachments.Add(new Attachment("1.txt"));
                msg.Attachments.Add(new Attachment("2.jpg"));
                client.Send(msg);
                return true;
            }
            catch
            {
                return false;
            }
        }

1 comment:

  1. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete