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()
                var client = new SmtpClient("", 587)
                    Credentials = new NetworkCredential("", "mypassword"),
                    EnableSsl = true
                var msg = new MailMessage("",
                    "Email subject",
                    "This is the email body...");
                msg.Attachments.Add(new Attachment("1.txt"));
                msg.Attachments.Add(new Attachment("2.jpg"));
                return true;
                return false;

Sending email with attachments using Delphi, Indy 10.5.5 and GMail

Sending email using Delphi is not difficult, you just need to know the carious bits and pieces that go together to make it happen. Over the years Indy has changed its classes and methods so your version of Indy might function slightly different to mine, Indy 10.5.5, its just the one that I have to hand right now. I'm also using Rad Studio 2010. To send email using GMail and Indy you'll need to download the Open SSL libraries. This is because GMail sensibly requires a secure connection while sending, and also receiving email.

You can download the Open SSL libraries here: (32-bit builds only at time of writing). You need to place 2 of the files contained in the download (libeay32.dll, and ssleay32.dll) into your system's path or just place them into the same directory as the executable you are building/running. Now for the Delphi part.