///
/// Prepares and sends an EMail
///
/// Recipient(s), multiple recipients should be separated by ;
/// Sender address
/// CC address
/// Subject of Mail
/// Body of Mail
/// Which server to use to send mail
/// Boolean, indicating if authentication required for this server
/// String, specifies UserName to login to SMTP Server
/// String, specifies Password to login to SMTP Server
/// Success status(true/false)
public static bool SendEMail(string sToAddress, string sFromAddress, string sCC, string sSubject,
string sMessage, string sSMTPServer, bool bIsAuthReq, string sUserName, string sPassword)
{
MailMessage mmMessage=new MailMessage();
mmMessage.To=sToAddress;
mmMessage.From=sFromAddress;
if (sCC.Length>0)
mmMessage.Cc=sCC;
mmMessage.Subject=sSubject;
mmMessage.Priority=MailPriority.High;
mmMessage.BodyFormat=MailFormat.Html;
mmMessage.Body=sMessage;
//Check whether target SMTP server requires authentication.
//If yes, get authentication details from configuration settings.
if (bIsAuthReq)
{
mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sUserName); //set username
mmMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sPassword); //Set password
}
try
{
SmtpMail.SmtpServer=sSMTPServer;
SmtpMail.Send(mmMessage);
return(true);
}
catch(Exception ex)
{
String sErr=ex.Message;
return(false);
}
}
|