The possible reason may be that it could not access 'CDO.Message'
object.
/*------------------ Exception details-----------------------*/
Could not access 'CDO.Message' object. --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.InteropServices.COMException (0x80040212): The transport
lost its connection to the server.
I used thecode as follows to send mail, and it works fine at all times.
Enjoy it.
/*---------------------------------------------------- Code
-------------------------------------------*/
/// <summary>
/// MailHandler class for sending mail notification.
/// </summary>
public class MailHandler
{
#region "Private members"
// Private variable members
private string _accountName = string.Empty; // Account name from
SMTP server.
private string _accountPwd = string.Empty; // Account password
from SMTP server.
private string _from = string.Empty; // Sender Email address.
private string _to = string.Empty; // Recipients Email address.
private string _subject = string.Empty; // Subject of the mail.
private string _body = string.Empty; // Body of the mail.
private string _mailServer = string.Empty; // Email server
name.
private bool _isRequireAuthentication = false; // Authentication.
private MailFormat _mailFormat = MailFormat.Html; // Email format.
#endregion
#region "Public properties"
/// <summary>
/// AccountName property for setting or getting account name from
SMTP server.
/// </summary>
public string AccountName
{
get { return this._accountName;}
set { this._accountName = value;}
}
/// <summary>
/// AccountPwd property for setting or getting account password from
SMTP server.
/// </summary>
public string AccountPwd
{
get { return this._accountPwd;}
set { this._accountPwd = value;}
}
/// <summary>
/// IsRequireAuthentication property for setting or getting
authentication.
/// </summary>
public bool IsRequireAuthentication
{
get { return this._isRequireAuthentication;}
set { this._isRequireAuthentication = value;}
}
/// <summary>
/// From property for setting or getting sender Email address.
/// </summary>
public string From
{
get { return this._from;}
set { this._from = value;}
}
/// <summary>
/// To property for setting or getting recipients Email address.
/// </summary>
public string To
{
get { return this._to;}
set { this._to = value;}
}
/// <summary>
/// Subject property for setting or getting the subject of the Email.
/// </summary>
public string Subject
{
get { return this._subject;}
set { this._subject = value;}
}
/// <summary>
/// Body property for setting or getting the body of the Email.
/// </summary>
public string Body
{
get { return this._body;}
set { this._body = value;}
}
/// <summary>
/// MailServer property for setting or getting email server name.
/// </summary>
public string MailServer
{
get { return this._mailServer;}
set { this._mailServer = value;}
}
#endregion
#region "Constructors"
/// <summary>
/// Default constructor
/// </summary>
public MailHandler()
{
this._from = string.Empty;
this._to = string.Empty;
this._subject = string.Empty;
this._body = string.Empty;
this._mailServer = string.Empty;
this._mailFormat = MailFormat.Html;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="from">Sender Email address</param>
/// <param name="to">Recipients Email address</param>
/// <param name="subject">Subject of the mail</param>
/// <param name="body">Body of the mail</param>
/// <param name="mailServer">Email server name</param>
public MailHandler(string from,string to,string subject,string body,
string mailServer,string accountName,string accountPwd,bool
isRequireAuthentication)
{
this._from = from;
this._to = to;
this._subject = subject;
this._body = body;
this._mailServer = mailServer;
this._mailFormat = MailFormat.Html;
this._accountName = accountName;
this._accountPwd = accountPwd;
this._isRequireAuthentication = isRequireAuthentication;
}
#endregion
#region "Public functions"
/// <summary>
/// SendMail method is used to send email.
/// </summary>
public bool SendMail()
{
bool isSended = false;
try
{
// Construct Email content.
MailMessage notificationMail = new MailMessage();
notificationMail.From = this._from;
notificationMail.To = this._to;
notificationMail.Subject = this._subject;
notificationMail.Priority = MailPriority.Normal;
notificationMail.BodyFormat = this._mailFormat;
notificationMail.Body = this._body;
// Set SMTP server address.
SmtpMail.SmtpServer = this._mailServer;
// Set authentication.
if (this._isRequireAuthentication)
{
notificationMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
"1");
// Set Account information from SMTP server.
notificationMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername",this._accountName);
notificationMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword",this._accountPwd);
}
// Send mail.
SmtpMail.Send(notificationMail);
isSended = true;
}
catch (Exception e)
{
isSended = false;
throw new Exception(e.Message,e);
}
return isSended;
}
#endregion
}