Sending Email from .NET

I have created an Email Class in C# which can be called from other applications to send email using SMTP protocol. We may create a C# DLL based on the following class and then add reference of the dll in the calling application.

using System;

using System.Collections.Generic;

using System.Text;

using System.Net.Mail;

using System.Net;

using System.IO;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace FazlulsEmail

{

public class Email

{

public string sendEmail(string[] toAddressList, string[] ccAddressList, string[] bccAddressList, string subject, string body, string fromAddress, bool isHTML)

{

MailMessage message = new MailMessage(new MailAddress(fromAddress), new MailAddress(toAddressList[0]));

for (int i = 1; i < toAddressList.Length; i++)

{

message.To.Add(new MailAddress(toAddressList[i]));

}

for (int j = 1; j < ccAddressList.Length; j++)

{

message.CC.Add(new MailAddress(ccAddressList[j]));

}

for (int k = 1; k < bccAddressList.Length; k++)

{

message.Bcc.Add(new MailAddress(bccAddressList[k]));

}

message.Subject = subject;

message.Body = body;

message.IsBodyHtml = isHTML;

try

{

//Mail Server: mySMTPMailServer and Port: 191

SmtpClient client = new SmtpClient("mySMTPMailServer"191);

client.UseDefaultCredentials = true;

client.Send(message);

return "Email Sent Successfully";

}

catch (Exception ex)

{

return ex.Message;

}

}

//Use the following 2 lines to call from Client Application

//FazlulsEmail.Email _EmailCenter = new FazlulsEmail.Email();

//_EmailCenter.sendEmail(address, "My Subject", body, "From Address", attachments.stream, attachments.filename, true);

//Multiple Stream[] contentStream, string[] filename

public string sendEmail(string[] toAddressList, string[] ccAddressList, string[] bccAddressList, string subject, string body, string fromAddress, Stream contentStream, string filename, bool isHTML)

{

MailMessage message = new MailMessage(new MailAddress(fromAddress), new MailAddress(toAddressList[0]));

for (int i = 1; i < toAddressList.Length; i++)

{

message.To.Add(new MailAddress(toAddressList[i]));

}

for (int j = 1; j < ccAddressList.Length; j++)

{

message.CC.Add(new MailAddress(ccAddressList[j]));

}

for (int k = 1; k < bccAddressList.Length; k++)

{

message.Bcc.Add(new MailAddress(bccAddressList[k]));

}

message.Subject = subject;

message.Body = body;

message.IsBodyHtml = isHTML;

//Multiple Stream[] contentStream, string[] filename

//for (int i = 0; i < contentStream.Length; i++)

// message.Attachments.Add(new Attachment(contentStream[i], filename[i]));

message.Attachments.Add(new Attachment(contentStream, filename));

try

{

//Mail Server: mySMTPMailServer and Port: 191

SmtpClient client = new SmtpClient("mySMTPMailServer"191);

client.UseDefaultCredentials = true;

client.Send(message);

return "Email Sent Successfully";

}

catch (Exception ex)

{

return ex.Message;

}

}

}

}

Comments

Popular posts from this blog

How to Create SSRS Report on SharePoint List

Database Testing With DBUnit

SAP RFC Integration through BizTalk