Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Last active August 29, 2015 14:06
Show Gist options
  • Save pinalbhatt/48f625bbffcdf8849f4a to your computer and use it in GitHub Desktop.
Save pinalbhatt/48f625bbffcdf8849f4a to your computer and use it in GitHub Desktop.
SendEmail
//using System.Net;
//using System.Net.Mail;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("<smtp host address>");
mail.From = new MailAddress("me@mydomain.com");
mail.To.Add("u@urdomain.com");
mail.Subject = "Subject";
mail.Body = "body";
SmtpServer.Port = 25; // Or whatever port your smpt server is supporting
SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
using System.Net.Mail;
public static bool Send(MailMessage email)
{
var result = false;
if (email != null && email.To.Count > 0)
{
using (SmtpClient smtp = new SmtpClient())
{
try
{
email.From = new MailAddress("<from email address>");
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("<from email id>", "<password>");
smtp.EnableSsl = true;
smtp.Send(email);
result = true;
}
catch(Exception ex)
{
//throw new Exception("Error in EmailMgr.Send(MailMessage). " + ex.Message, ex);
result = false;
}
}
}
return result;
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment