//Simple Email Sending function in C# using Gmail
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Configuration;
///
/// Summary description for Email
///
namespace Tools
{
public class Email
{
public Email()
{
//
// TODO: Add constructor logic here
//
}
public static bool SendMail(string to, string subject, string message)
{
try
{
string gMailAccount;
string password;
gMailAccount = ConfigurationManager.AppSettings.Get("emailUserName");
password = ConfigurationManager.AppSettings.Get("emailPassword");
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
System.Net.Mail.MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
//Attachment msgAttachment = new Attachment("g:\\test.doc");
//msg.Attachments.Add(msgAttachment);
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
No comments:
Post a Comment