Last active
August 29, 2015 14:06
-
-
Save pinalbhatt/48f625bbffcdf8849f4a to your computer and use it in GitHub Desktop.
SendEmail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using System.Net; | |
//using System.Net.Mail; | |
MailMessage mail = new MailMessage(); | |
SmtpClient SmtpServer = new SmtpClient("<smtp host address>"); | |
mail.From = new MailAddress("[email protected]"); | |
mail.To.Add("[email protected]"); | |
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MailMessage mail = new MailMessage(); | |
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); | |
mail.From = new MailAddress("[email protected]"); | |
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