Skip to content

Instantly share code, notes, and snippets.

@makafanpeter
Created January 3, 2015 21:10
Show Gist options
  • Save makafanpeter/b19554e71a6935da0c1d to your computer and use it in GitHub Desktop.
Save makafanpeter/b19554e71a6935da0c1d to your computer and use it in GitHub Desktop.
using System;
using System.Net.Mail;
using System.Threading.Tasks;
namespace AcePickup.Helpers
{
public interface IMailApp:IDisposable
{
/// <summary>
/// This service allows users to send emails with complete control over the content of the email.
/// </summary>
/// <param name="to">the address of the recipient</param>
/// <param name="subject">the subject of the email</param>
/// <param name="ccMail">a comma-separated list of email addresses to CC</param>
/// <param name="body">the body of the email in plain text</param>
/// <param name="from">the address of the sender</param>
/// <returns>true/false</returns>
bool send(string[] to, string subject, string[] ccMail, string body,string from = "[email protected]");
}
public class MailApp : IMailApp
{
MailMessage Message;
SmtpClient smtpClient;
public MailApp()
{
Message = new MailMessage();
smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
Message.IsBodyHtml = true;
}
/// <summary>
/// This service allows users to send emails with complete control over the content of the email.
/// </summary>
/// <param name="to">the address of the recipients</param>
/// <param name="subject">the subject of the email</param>
/// <param name="ccMail">a array list of email addresses to CC</param>
/// <param name="body">the body of the email in plain text/HTML</param>
/// <param name="from">the address of the sender</param>
/// <returns>true/false</returns>
public bool send(string[] to, string subject, string[] ccMail, string body, string from = "[email protected]")
{
try
{
Message.From = new MailAddress(from);
Message.Subject = subject;
Message.Body = body;
Message.To.Clear();
Message.CC.Clear();
foreach (var email in to)
{
Message.To.Add(email);
}
foreach (var email in ccMail)
{
Message.CC.Add(email);
}
if (to.Length > 0)
{
//smtpClient.Send(Message);
return true;
}
return false;
}
catch (Exception ex)
{
return false;
}
}
public void Dispose()
{
smtpClient.Dispose();
Message.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment