Jarloo

Menu

Gmail in C#

Use C# to send mail using Google’s Gmail. If you’ve ever wanted to send email using your Gmail account then take a look.

 

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

namespace JarlooGmail
{
    public class Gmail
    {
        public string Username { get; set; }
        public string Password { get; set; }

        public Gmail(string username, string password)
        {
            Username = username;
            Password = password;
        }

        public void Send(MailMessage msg)
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(Username, Password);
            client.Send(msg);
        }
    }
}

You can call the Gmail c# class like so:

using System.Net.Mail;

namespace JarlooGmail
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Gmail gmail = new Gmail("MyUsername","MyPassword");
            MailMessage msg = new MailMessage("FromMe@here", "ToYou@there");
            msg.Subject = "my subject";
            msg.Body = "the body of the message";
            gmail.Send(msg);
        }
    }
}

Categories:   Code

Tags:  , ,

Comments

Sorry, comments are closed for this item.