Skip to content

Instantly share code, notes, and snippets.

@hclewk
Created December 31, 2016 18:01
Show Gist options
  • Save hclewk/0ef1469b31a06fbcb8aca9df0457fa97 to your computer and use it in GitHub Desktop.
Save hclewk/0ef1469b31a06fbcb8aca9df0457fa97 to your computer and use it in GitHub Desktop.
Send Email from Gmail
public class Email
{
public string Subject { get; set; }
public string Body { get; set; }
public string To { get; set; }
public string From { get; set; }
public Email()
{
}
public Email(string from, string to, string subject, string body)
{
From = from;
To = to;
Subject = subject;
Body = body;
}
public bool Send()
{
MailMessage m = new MailMessage();
m.To.Add(new MailAddress(To));
m.From = new MailAddress(From);
m.Subject = Subject;
m.Body = Body;
m.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("user@domain.com", "password");
client.Port = 587;//or use 465
client.Host = "smtp.gmail.com";
object userState = m;
client.Send(m);
}
catch (Exception)
{
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment