Sending Email:-

HI,

The following Class file contains a generic function used to send email.
I have also attached the documentation of this Class file and the file SendEmail.cs.



using System;
using System.Data;
using System.Configuration;
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;
using System.Collections;
using System.IO;
using System.Text;
using System.Net.Mail;


///
/// Summary description for SendEmail
///

///

public class SendEmail
{
#region Private Fields

private string host = string.Empty;
private int port = 0;
private string username = string.Empty;
private string pwd = string.Empty;
private string fromAddress = string.Empty;
private string fromDisplayName = string.Empty;
private string bodyMsg = string.Empty;
private string subject = string.Empty;
private Hashtable toAddressHash;
private ArrayList arrCC;
private ArrayList arrBCC;
private string path = string.Empty;

#endregion

#region Public Properties

public string Host
{
set
{
host = value;
}
}
public int Port
{
set
{
port = value;
}
}

public string Username
{
set
{
username = value;
}
}
public string Password
{
set
{
pwd = value;
}
}
public string FromAddress
{
set
{
fromAddress = value;
}
}
public string FromDisplayName
{
set
{
fromDisplayName = value;
}
}
public string Message
{
set
{
bodyMsg = value;
}
}
public string Subject
{
set
{
subject = value;
}
}
public Hashtable ToAddress
{
set
{
toAddressHash = new Hashtable();
toAddressHash = value;
}
}
public ArrayList CC
{
set
{
arrCC = new ArrayList();
arrCC = value;
}
}
public ArrayList BCC
{
set
{
arrBCC = new ArrayList();
arrBCC = value;
}
}
public string Path
{
set
{
path = value;
}
}
#endregion

public SendEmail()
{
//
// TODO: Add constructor logic here
//
}

// To Send Email:
// Input Parameters : Hostname,Port,Username,Password,FromAddress,DisplayName of the FromAddress,Message to be sent in the body section of the email,Subject of the email,Hashtable containing ToAddresses and the corresponding Displaynames,ArrayList containg CarbonCopy Addresses, ArrayList containg Blind CarbonCopy Adrressses,Attachment Pathname.
// Returns nothing.

public void EmailAlert()
{
try
{
SmtpClient client = new SmtpClient(host, port);

if (username != "" && pwd != "")
{
System.Net.NetworkCredential myCache = new System.Net.NetworkCredential(username, pwd);
client.Credentials = myCache;
}

MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress, fromDisplayName);
if (toAddressHash != null)
{
if (toAddressHash.Count > 0)
{
foreach (string key in toAddressHash.Keys)
{
message.To.Add(new MailAddress(key, toAddressHash[key].ToString()));
}
}
}
if (arrCC != null)
{
if (arrCC.Count > 0)
{
foreach (string CCaddress in arrCC)
{
message.CC.Add(CCaddress);
}
}
}
if (arrBCC != null)
{
if (arrBCC.Count > 0)
{
foreach (string BCCaddress in arrBCC)
{
message.Bcc.Add(BCCaddress);
}
}
}
if (bodyMsg != "")
{
message.Body = bodyMsg;
}
if (subject != "")
{
message.Subject = subject;
}

message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
message.Priority = MailPriority.Normal;

if (path != "")
{
Attachment attachFile = new Attachment(path);
message.Attachments.Add(attachFile);
}

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
message.Dispose();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message.ToString());
}
}
}


Thanks


Attachments

  • Documentation (30383-14034-SendEmail.doc)
  • Cs file (30383-14034-SendEmail.cs.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: