//This is my new post for sending emails using C#.net and Gmail with Attachments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Configuration;
//using System.Web.Mail;
///
/// 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, string CSVAttachmentList)
{
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;
string[] filesToAttch = CSVAttachmentList.Split(',');
foreach (string fileName in filesToAttch)
{
Attachment msgAttachment = new Attachment(fileName);
msg.Attachments.Add(msgAttachment);
}
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
No comments:
Post a Comment