C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Sending Email:-


Posted Date: 14 Jul 2009    Resource Type: Code Snippets    Category: Email
Author: Priyadarsini MMember Level: Gold    
Rating: 1 out of 5Points: 15 (Rs 10)



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)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    SendEmail  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Send Email
    Previous Resource: Code to send email
    Return to Discussion Resource Index
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use