Using system;
Using system. IO;
// Yang Chun
// 2008-6-8
/// <Summary>
/// Mail Summary
/// </Summary>
Public class mymail
{
String publicmail = system. configuration. configurationsettings. deleetask[ "publicmail"]. tostring ();
String pubicmmailpassword = system. configuration. configurationsettings. deleetask[ "publicmailpasword"]. tostring ();
String mailusername = system. configuration. configurationsettings. receivettings ["mailusername"]. tostring ();
Public mymail ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "reciver"> recipient </param>
/// <Param name = "title"> topic </param>
/// <Param name = "content"> content </param>
/// <Param name = "attpath"> attachment </param>
/// <Returns> Successful </returns>
Public bool Sendmail (string reciver, String title, string content, string attpath)
{
Try
{
If ("" = attpath)
{
Return sendmailbyjmail (reciver, title, content );
}
Else
{
If (file. exists (attpath) = true)
{
Return sendmailbyjmail (reciver, title, content, attpath );
}
Else
{
Return sendmailbyjmail (reciver, title, content );
}
}
}
Catch (exception ex)
{
Return false;
Console. Write (ex. Message );
}
}
/// <Summary>
/// Send an email without attachment
/// </Summary>
/// <Param name = "reciver"> recipient </param>
/// <Param name = "title"> topic </param>
/// <Param name = "content"> content </param>
/// <Returns> Successful </returns>
///
Private bool sendmailbyjmail (string reciver, String title, string content)
{
Myjmail. messageclass jmailmessage = new myjmail. messageclass ();
Jmailmessage. charset = "gb2312 ";
Jmailmessage. Encoding = "base64 ";
Jmailmessage. contenttype = "text/html ";
Jmailmessage. isoencodeheaders = false;
Jmailmessage. Priority = convert. tobyte (1 );
Jmailmessage. From = publicmail;
Jmailmessage. fromname = mailusername;
Jmailmessage. Subject = title;
Jmailmessage. mailserverusername = getloginnamebyemail (publicmail );
Jmailmessage. mailserverpassword = pubicmmailpassword;
Jmailmessage. addrecipient (reciver ,"","");
Jmailmessage. Body = content;
If (jmailmessage. Send (getsmtpsererbyemail (publicmail), false ))
{
Return true;
}
Else
{
Return false;
}
}
/// <Summary>
/// Send an email with an attachment
/// </Summary>
/// <Param name = "reciver"> recipient </param>
/// <Param name = "title"> topic </param>
/// <Param name = "content"> content </param>
/// <Param name = "attpath"> attachment path </param>
/// <Returns> Successful </returns>
Private bool sendmailbyjmail (string reciver, String title, string content, string attpath)
{
Myjmail. messageclass jmailmessage = new myjmail. messageclass ();
Jmailmessage. charset = "gb2312 ";
Jmailmessage. Encoding = "base64 ";
Jmailmessage. contenttype = "text/html ";
Jmailmessage. isoencodeheaders = false;
Jmailmessage. Priority = convert. tobyte (1 );
Jmailmessage. From = publicmail;
Jmailmessage. fromname = mailusername;
Jmailmessage. Subject = title;
Jmailmessage. mailserverusername = getloginnamebyemail (publicmail );
Jmailmessage. mailserverpassword = pubicmmailpassword;
Jmailmessage. addrecipient (reciver ,"","");
Jmailmessage. addattachment (attpath, true, attpath. substring (attpath. lastindexof (".") + 1, 3 ));
Jmailmessage. Body = content;
If (jmailmessage. Send (getsmtpsererbyemail (publicmail), false ))
{
Return true;
}
Else
{
Return false;
}
}
///
// obtain the logon name based on the email address
///
// email
// logon name
private string getloginnamebyemail (string email)
{< br> string loginname = Email. substring (0, email. indexof ("@");
return loginname;
}
/// <Summary>
/// Obtain the SMTP server address by email
/// </Summary>
/// <Param name = "email"> email </param>
/// <Returns> stmp server address </returns>
Private string getsmtpsererbyemail (string email)
{
Int length = Email. length;
String smtpserver = "SMTP." + email. substring (email. indexof ("@") + 1, length-email. lastindexof ("@")-1 );
Return smtpserver;
}
}