Using system;
Using system. text;
Using system. IO;
Using system. net;
Using system. net. Sockets;
Using system. collections;
Namespace lion_office.data.library.mailclass
{
/*
Create by lion
Copyright (c) 2001,2002 www. lionsky. net. All rights reserved.
Web: http://www.Lionsky.net
Email: lion-a@sohu.com
Support. NET Framework beta 2
*/
Public class esmtpmail
{
Private string enter = "\ r \ n ";
/// <Summary>
/// Set the language Code The default value is gb2312, which can be set to "" if not required ""
/// </Summary>
Public String charset = "gb2312 ";
/// <Summary>
/// Sender address
/// </Summary>
Public String from = "";
/// <Summary>
/// Sender's name
/// </Summary>
Public String fromname = "";
/// <Summary>
/// Reply to the email address
/// </Summary>
// Public String replyto = "";
/// <Summary>
/// Recipient's name
/// </Summary>
Public String recipientname = "";
/// <Summary>
/// Recipient List
/// </Summary>
Private hashtable recipient = new hashtable ();
/// <Summary>
/// Email server Domain Name
/// </Summary>
Private string MailServer = "";
/// <Summary>
/// Email server port number
/// </Summary>
Private int mailserverport = 25;
/// <Summary>
/// User name used for SMTP authentication
/// </Summary>
Private string username = "";
/// <Summary>
/// Password used for SMTP authentication
/// </Summary>
Private string Password = "";
/// <Summary>
/// Whether SMTP authentication is required
/// </Summary>
Private bool ESMTP = false;
/// <Summary>
/// Whether the email is html
/// </Summary>
Public bool html = false;
/// <Summary>
/// Email attachment list
/// </Summary>
Private system. Collections. arraylist attachments;
/// <Summary>
/// Mail sending priority, which can be set to "high", "normal", "low" or "1", "3", "5"
/// </Summary>
Private string priority = "normal ";
/// <Summary>
/// Email Subject
/// </Summary>
Public String subject = "";
/// <Summary>
/// Body of the email
/// </Summary>
Public String body = "";
/// <Summary>
/// Number of recipients
/// </Summary>
Private int recipientnum = 0;
/// <Summary>
/// Maximum number of recipients
/// </Summary>
Private int recipientmaxnum = 1;
/// <Summary>
/// Number of BCC recipients
/// </Summary>
// Private int recipientbccnum = 0;
/// <Summary>
/// Error message feedback
/// </Summary>
Private string errmsg;
/// <Summary>
/// Tcpclient object, used to connect to the server
/// </Summary>
Private tcpclient TC;
/// <Summary>
/// Networkstream object
/// </Summary>
Private networkstream ns;
/// <Summary>
/// SMTP error code hash table
/// </Summary>
Private hashtable errcodeht = new hashtable ();
/// <Summary>
/// SMTP hash table with correct code
/// </Summary>
Private hashtable rightcodeht = new hashtable ();
Public esmtpmail ()
{
Attachments = new system. Collections. arraylist ();
}
/// <Summary>
/// Email server domain name and verification information
/// Form such as: "User: pass@www.server.com: 25", secondary information can also be omitted. For example, "User: pass@www.server.com" or "www.server.com"
/// </Summary>
Public String maildomain
{
Set
{
String maidomain = value. Trim ();
Int tempint;
If (maidomain! = "")
{
Tempint = maidomain. indexof ("@");
If (tempint! =-1)
{
String STR = maidomain. substring (0, tempint );
Mailserverusername = Str. substring (0, str. indexof (":"));
Mailserverpassword = Str. substring (Str. indexof (":") + 1, str. Length-str.IndexOf (":")-1 );
Maidomain = maidomain. substring (tempint + 1, maidomain. Length-tempint-1 );
}
Tempint = maidomain. indexof (":");
If (tempint! =-1)
{
MailServer = maidomain. substring (0, tempint );
Mailserverport = system. Convert. toint32 (maidomain. substring (tempint + 1, maidomain. Length-tempint-1 ));
}
Else
{
MailServer = maidomain;
}
}
}
}
/// <Summary>
/// Email server port number
/// </Summary>
Public int maildomainport
{
Set
{
Mailserverport = value;
}
}
/// <Summary>
/// User name used for SMTP authentication
/// </Summary>
Public String mailserverusername
{
Set
{
If (value. Trim ()! = "")
{
Username = value. Trim ();
ESMTP = true;
}
Else
{
Username = "";
ESMTP = false;
}
}
}
/// <Summary>
/// Password used for SMTP authentication
/// </Summary>
Public String mailserverpassword
{
Set
{
Password = value;
}
}
/// <Summary>
/// Mail sending priority, which can be set to "high", "normal", "low" or "1", "3", "5"
/// </Summary>
Public String priority
{
Set
{
Switch (value. tolower ())
{
Case "high ":
Priority = "high ";
Break;
Case "1 ":
Priority = "high ";
Break;
Case "normal ":
Priority = "normal ";
Break;
Case "3 ":
Priority = "normal ";
Break;
Case "low ":
Priority = "low ";
Break;
Case "5 ":
Priority = "low ";
Break;
Default:
Priority = "normal ";
Break;
}
}
}
/// <Summary>
/// Error message feedback
/// </Summary>
Public String errormessage
{
Get
{
Return errmsg;
}
}
/// <Summary>
/// Server interaction record
/// </Summary>
Private string logs = "";
///
// server interaction record. If you find that the SMTP server cannot be used by this component, please send me the logs at the time of error (lion-a@sohu.com) And I will identify the cause as soon as possible.
//
Public String logs
{< br> Get
{< br> return logs;
}< BR >}
///
// SMTP response code hash table
///
private void smtpcodeadd ()
{< br> errcodeht. add ("500", "email address error");
errcodeht. add ("501", "parameter format error");
errcodeht. add ("502", "command cannot be implemented");
errcodeht. add ("503", "the server requires SMTP authentication");
errcodeht. add ("504", "command parameters cannot be implemented");
errcodeht. add ("421", "service not ready, disable transmission channel");
errcodeht. add ("450", "requested email operations are not completed, mailbox is unavailable (for example, mailbox busy)");
errcodeht. add ("550", "the requested email operation is incomplete, and the email address is unavailable (for example, the email address is not found, or cannot be accessed)");
errcodeht. add ("451", "discard requested operations; errors during processing");
errcodeht. add ("551", "non-local user, please try ");
errcodeht. add ("452", "the operation is not executed due to insufficient system storage");
errcodeht. add ("552", "excessive storage allocation, requested operation not executed");
errcodeht. add ("553", "The mailbox name is unavailable, the required operation is not performed (for example, the mailbox format is incorrect)");
errcodeht. add ("432", "requires a password conversion");
errcodeht. add ("534", "authentication mechanism is too simple");
errcodeht. add ("538", "encryption is required for the authentication mechanism currently requested");
errcodeht. add ("454", "temporary authentication failed");
errcodeht. add ("530", "authentication required");
Rightcodeht. Add ("220", "Service ready ");
Rightcodeht. Add ("250", "required email operation completed ");
Rightcodeht. Add ("251", "the user is not local and will forward to <forward-path> ");
Rightcodeht. Add ("354", "Start email input, with <enter>. <enter> end ");
Rightcodeht. Add ("221", "");
Rightcodeht. Add ("334", "Server Response verification base64 string ");
Rightcodeht. Add ("235", "verified ");
}
/// <Summary>
/// Encode the string as a base64 string
/// </Summary>
/// <Param name = "estr"> string to be encoded </param>
Private string base64encode (string Str)
{
Byte [] barray;
Barray = encoding. Default. getbytes (STR );
Return convert. tobase64string (barray );
}
/// <Summary>
/// Decodes a base64 string into a normal string
/// </Summary>
/// <Param name = "dstr"> string to be decoded </param>
Private string base64decode (string Str)
{
Byte [] barray;
Barray = convert. frombase64string (STR );
Return encoding. Default. getstring (barray );
}
/// <Summary>
/// Get the file stream of the uploaded attachment
/// </Summary>
/// <Param name = "filepath"> absolute path of the attachment </param>
Private string getstream (string filepath)
{
// Create a file stream object
System. Io. filestream filestr = new system. Io. filestream (filepath, system. Io. filemode. Open );
Byte [] by = new byte [system. Convert. toint32 (filestr. Length)];
Filestr. Read (by, 0, by. Length );
Filestr. Close ();
Return (system. Convert. tobase64string ());
}
/// <Summary>
/// Add email attachments
/// </Summary>
/// <Param name = "path"> absolute path of the attachment </param>
Public void addattachment (string path)
{
Attachments. Add (PATH );
}
/// <Summary>
/// Add a recipient
/// </Summary>
/// <Param name = "str"> recipient address </param>
Public bool addrecipient (string Str)
{
STR = Str. Trim ();
If (STR = NULL | STR = "" | Str. indexof ("@") =-1)
Return true;
If (recipientnum <recipientmaxnum)
{
Recipient. Add (recipientnum, STR );
Recipientnum ++;
Return true;
}
Else
{
Errmsg + = "too many recipients ";
Return false;
}
}
/// <Summary>
/// Maximum number of recipients
/// </Summary>
Public int recipientmaxnum
{
Set
{
Recipientmaxnum = value;
}
}
/// <Summary>
/// Add a group of recipients (no more than recipientmaxnum). The parameter is a string array.
/// </Summary>
/// <Param name = "str"> Save the string array with the recipient address (no more than recipientmaxnum) </param>
Public bool addrecipient (string [] Str)
{
For (INT I = 0; I <Str. length; I ++)
{
If (! Addrecipient (STR [I])
{
Return false;
}
}
Return true;
}
/// <Summary>
/// Send the SMTP command
/// </Summary>
Private bool sendcommand (string Str)
{
Byte [] writebuffer;
If (STR = NULL | Str. Trim () = "")
{
Return true;
}
Logs + = STR;
Writebuffer = encoding. Default. getbytes (STR );
Try
{
NS. Write (writebuffer, 0, writebuffer. Length );
}
Catch
{
Errmsg = "network connection error ";
Return false;
}
Return true;
}
/// <Summary>
/// Receive the SMTP server response
/// </Summary>
Private string recvresponse ()
{
Int streamsize;
String returnvalue = "";
Byte [] readbuffer = new byte [1024];
Try
{
Streamsize = NS. Read (readbuffer, 0, readbuffer. Length );
}
Catch
{
Errmsg = "network connection error ";
Return "false ";
}
If (streamsize = 0)
{< br> return returnvalue;
}< br> else
{< br> returnvalue = encoding. default. getstring (readbuffer ). substring (0, streamsize);
logs + = returnvalue;
return returnvalue;
}< BR >}
/// <Summary>
/// Interact with the server, send a command, and receive a response.
/// </Summary>
/// <Param name = "command"> a command to be sent </param>
/// <Param name = "errstr"> information to be reported if an error occurs </param>
Private bool Dialog (string STR, string errstr)
{
If (STR = NULL | Str. Trim () = "")
{
Return true;
}
If (sendcommand (STR ))
{
String RR = recvresponse ();
If (RR = "false ")
{
Return false;
}
String rrcode = RR. substring (0, 3 );
If (rightcodeht [rrcode]! = NULL)
{
Return true;
}
Else
{
If (errcodeht [rrcode]! = NULL)
{
Errmsg + = (rrcode + errcodeht [rrcode]. tostring ());
Errmsg + = enter;
}
Else
{
Errmsg + = RR;
}
Errmsg + = errstr;
Return false;
}
}
Else
{
Return false;
}
}
/// <Summary>
/// Interact with the server, send a group of commands, and receive responses.
/// </Summary>
Private bool Dialog (string [] STR, string errstr)
{
For (INT I = 0; I <Str. length; I ++)
{
If (! Dialog (STR [I], "")
{
Errmsg + = enter;
Errmsg + = errstr;
Return false;
}
}
Return true;
}
Private bool sendemail ()
{
// Connect to the network
Try
{
TC = new tcpclient (MailServer, mailserverport );
}
Catch (exception E)
{
Errmsg = E. tostring ();
Return false;
}
NS = tc. getstream ();
Smtpcodeadd ();
// Verify that the network connection is correct
If (rightcodeht [recvresponse (). substring (0, 3)] = NULL)
{
Errmsg = "network connection failed ";
Return false;
}
String [] sendbuffer;
String sendbufferstr;
// Perform SMTP Verification
If (ESMTP)
{
Sendbuffer = new string [4];
Sendbuffer [0] = "ehlo" + MailServer + enter;
Sendbuffer [1] = "auth login" + enter;
Sendbuffer [2] = base64encode (username) + enter;
Sendbuffer [3] = base64encode (password) + enter;
If (! Dialog (sendbuffer, "SMTP server Verification Failed. Check the user name and password. "))
Return false;
}
Else
{
Sendbufferstr = "HELO" + MailServer + enter;
If (! Dialog (sendbufferstr ,""))
Return false;
}
//
Sendbufferstr = "mail from: <" + from + ">" + enter;
If (! Dialog (sendbufferstr, "the sender address is incorrect, or cannot be blank "))
Return false;
//
Sendbuffer = new string [recipientmaxnum];
For (INT I = 0; I <recipient. Count; I ++)
{
Sendbuffer [I] = "rcpt to: <" + recipient [I]. tostring () + ">" + enter;
}
If (! Dialog (sendbuffer, "the recipient address is incorrect "))
Return false;
Sendbufferstr = "data" + enter;
If (! Dialog (sendbufferstr ,""))
Return false;
Sendbufferstr = "from:" + fromname + "<" + from + ">" + enter;
Sendbufferstr + = "to: =? "+ Charset. toupper () + "? B? "+ Base64encode (recipientname) + "? = "+" <"+ Recipient [0] +"> "+ enter;
Sendbufferstr + = "cc :";
For (INT I = 0; I <recipient. Count; I ++)
{
Sendbufferstr + = recipient [I]. tostring () + "<" + recipient [I]. tostring () + "> ,";
}
Sendbufferstr + = enter;
If (charset = "")
{
Sendbufferstr + = "Subject:" + Subject + enter;
}
Else
{
Sendbufferstr + = "Subject:" + "=? "+ Charset. toupper () + "? B? "+ Base64encode (subject) + "? = "+ Enter;
}
Sendbufferstr + = "X-priority:" + priority + enter;
Sendbufferstr + = "X-msmail-priority:" + priority + enter;
Sendbufferstr + = "importance:" + priority + enter;
Sendbufferstr + = "X-mailer: huolx. pubclass" + enter;
Sendbufferstr + = "mime-version: 1.0" + enter;
Sendbufferstr + = "Content-Type: multipart/mixed;" + enter; // content format and delimiter
Sendbufferstr + = "boundary = \" ---- = _ nextpart_000_00d6_01c29593.aab31770 \ "" + enter;
Sendbufferstr + = "------ = _ nextpart_000_00d6_01c29593.aab31770" + enter;
If (HTML)
{
Sendbufferstr + = "Content-Type: text/html;" + enter;
}
Else
{
Sendbufferstr + = "Content-Type: text/plain;" + enter;
}
If (charset = "")
{
Sendbufferstr + = "charset = \" iso-8859-1 \ "" + enter;
}
Else
{
Sendbufferstr + = "charset = \" "+ charset. tolower () +" \ "" + enter;
}
// Sendbufferstr + = "content-transfer-encoding: base64" + enter;
Sendbufferstr + = "content-transfer-encoding: base64" + enter;
Sendbufferstr + = base64encode (body) + enter;
If (attachments. Count! = 0)
{
Foreach (string filepath in attachments)
{
Sendbufferstr + = "------ = _ nextpart_000_00d6_01c29593.aab31770" + enter;
Sendbufferstr + = "Content-Type: Application/octet-stream" + enter;
Sendbufferstr + = "name = \" =? "+ Charset. toupper () + "? B? "+ Base64encode (filepath. substring (filepath. lastindexof (" \ ") + 1) + "? = \ "" + Enter;
Sendbufferstr + = "content-transfer-encoding: base64" + enter;
Sendbufferstr + = "content-Disposition: attachment;" + enter;
Sendbufferstr + = "filename = \" =? "+ Charset. toupper () + "? B? "+ Base64encode (filepath. substring (filepath. lastindexof (" \ ") + 1) + "? = \ "" + Enter;
Sendbufferstr + = getstream (filepath) + enter;
}
}
Sendbufferstr + = "------ = _ nextpart_000_00d6_01c29593.aab31770 --" + enter;
Sendbufferstr + = enter + "." + enter;
If (! Dialog (sendbufferstr, "error message "))
Return false;
Sendbufferstr = "quit" + enter;
If (! Dialog (sendbufferstr, "error during disconnection "))
Return false;
NS. Close ();
TC. Close ();
Return true;
}
/// <Summary>
/// Mail sending method. All parameters are set through attributes.
/// </Summary>
Public bool send ()
{
If (recipient. Count = 0)
{
Errmsg = "the recipient list cannot be blank ";
Return false;
}
If (MailServer. Trim () = "")
{
Errmsg = "SMTP server must be specified ";
Return false;
}
Return sendemail ();
}
/// <Summary>
/// Email sending Method
/// </Summary>
/// <Param name = "smtpserver"> SMTP server information, such as "username: password@www.smtpserver.com: 25", you can also remove some secondary information, such as "www.smtpserver.com" </param>
Public bool send (string smtpserver)
{
Maildomain = smtpserver;
Return send ();
}
/// <Summary>
/// Email sending Method
/// </Summary>
/// <Param name = "smtpserver"> SMTP server information, such as "username: password@www.smtpserver.com: 25", you can also remove some secondary information, such as "www.smtpserver.com" </param>
/// <Param name = "from"> sender mail address </param>
/// <Param name = "fromname"> sender name </param>
/// <Param name = "to"> recipient address </param>
/// <Param name = "toname"> recipient name </param>
/// <Param name = "html"> whether the email is HTML </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "body"> email body </param>
Public bool send (string smtpserver, string from, string fromname, string to, string toname, bool HTML, string subject, string body)
{
Maildomain = smtpserver;
From = from;
Fromname = fromname;
Addrecipient ();
Recipientname = toname;
Html = HTML;
Subject = subject;
Body = body;
Return send ();
}
}
}