Skip to content

Instantly share code, notes, and snippets.

@johnkors
Created January 31, 2017 08:19
Show Gist options
  • Save johnkors/9cd71d1b95555f1b086740c84018b875 to your computer and use it in GitHub Desktop.
Save johnkors/9cd71d1b95555f1b086740c84018b875 to your computer and use it in GitHub Desktop.
Sample of a gmail email-sender
using System;
using System.Net;
using System.Net.Mail;
public class Email
{
public string From;
public string To;
public string Subject;
public string Text;
}
public class SMTPGmail {
private readonly string _gmailUser;
private readonly string _gmailPassword;
public SMTPGmail(string gmailUser, string gmailPassword)
{
_gmailUser = gmailUser;
_gmailPassword = gmailPassword;
}
public Tuple<bool, string> SendEmail(Email email)
{
try
{
var oFromAddress = new MailAddress(_gmailUser, "Email From " + email.From);
var oToAddress = new MailAddress(email.To, "Email To " + email.To);
string fromPassword = _gmailPassword;
string subject = email.Subject;
string body = email.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(_gmailUser, _gmailPassword)
};
using (var message = new MailMessage(oFromAddress, oToAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
return new Tuple<bool, string>(true, String.Empty);
}
}
catch (Exception ex)
{
return new Tuple<bool, string>(false, ex.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment