Click to See Complete Forum and Search --> : sending email codes


sheerin
February 17th, 2005, 02:18 AM
i am developing an examination questions bank. I would like to send an email to the administrator everytime a lecturer clicks on 'Save Question'. Can someone help me out. I would really appreciate it. Thanx

Sukim
February 17th, 2005, 02:51 AM
You can use this Following

using System;
using System.Collections;
using System.Web.Mail;
namespace MailSender
{

/// <summary>
/// This enumerator is used for determining the type of mail that we need to send
/// mainly it mentions the types like SMTP/MAPI etc.
/// </summary>
public enum MailType
{
Smtp=0,
Oracle=1,
Mapi=2
}
/// <summary>
/// Summary description for Mailer.
/// </summary>
public class Mailer
{
/// <summary>
/// This method is used for sending mail from the user.
/// In the current implemenatation we have left off MailType with
/// only one type which is SMTP mail. We can change this in the future
/// to accomodate more methods of sending mail.
/// </summary>
public void SendMail(MailType MT,string strfrom,string strto,string strsubject,string strmessage)
{
try
{
//create an object of the type Mailmessage
MailMessage objMessage = new MailMessage();

//assign properties of the mail message from the arguments that we are taking in.
string strtomailaddress = GetUniqueEmails(strto);
if(strtomailaddress!=";")
{
objMessage.To = strtomailaddress;
objMessage.From = strfrom;
objMessage.Subject = strsubject;
//objMessage.BodyFormat = MailFormat.Html;
objMessage.Body = strmessage;

//configure the SMTP server from the configuration file.
SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["MailServer"].ToString();
//comment out when not in use
SmtpMail.Send(objMessage);
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method is used for sending mail from the user.
/// In the current implemenatation we have left off MailType with
/// only one type which is SMTP mail. We can change this in the future
/// to accomodate more methods of sending mail.
/// </summary>
public void SendMail(MailType MT,string strfrom,string strto,string strcc, string strsubject,string strmessage)
{
try
{
//create an object of the type Mailmessage
MailMessage objMessage = new MailMessage();

//assign properties of the mail message from the arguments that we are taking in.
objMessage.To = GetUniqueEmails(strto);
objMessage.Cc = GetUniqueEmails(strcc);
objMessage.From = strfrom;
objMessage.Subject = strsubject;
objMessage.Body = strmessage;

//configure the SMTP server from the configuration file.
SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["MailServer"].ToString();
//comment out when not in use
SmtpMail.Send(objMessage);
}
catch(Exception ex)
{
throw ex;
}
}
}
}


Please make sure to put the SMPT server address in the Config file.
you can use this as static functions also.



Example

try
{ Mailer objMail = new Mailer();
objMail.SendMail(MailSender.Smtp,strFromEmailId,strToEmailId,strSubject,strBody);
objMail=null;
}
catch
{
throw new ApplicationException("Unable to send mail");
}