Using system;
Using system. net;
Using system. net. Sockets;
Using system. collections;
Using system. configuration;
Using system. text;
Using system. xml;
Using system. IO;
Using system. Web;
Using system. Web. mail;
Namespace mail
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Public class msendmail
{
Private tcpclient tcpclt;
Private networkstream networkstm;
Private hashtable rightcodeht = new hashtable ();
Private string smtpservername;
Private int smtpserverport;
Private string username;
Private string password;
Private string;
Private string from;
Private string fromname;
Private string charset;
Private string recipientname;
Private string subject;
Private string body;
Private string priority;
Static string send_method;
Public msendmail ()
{
}
Public msendmail (string strtoname, string strto, string strbody)
{
To = strto;
Recipientname = strtoname;
Body = strbody;
Smtpcodeadd ();
}
Public msendmail (string strtoname, string strto, string strsubject, string strbody)
{
To = strto;
Recipientname = strtoname;
Subject = strsubject;
Body = strbody;
Smtpcodeadd ();
}
Public msendmail (string strtoname, string strto, string strfromname, string strfrom, string strsubject, string strbody)
{
To = strto;
Recipientname = strtoname;
From = strfrom;
Fromname = strfromname;
Subject = strsubject;
Body = strbody;
Smtpcodeadd ();
}
Private bool initialize ()
{
Try
{
If (send_method = "1 ")
{
Smtpservername = configurationsettings. receivettings ["smtpservername"];
Smtpserverport = convert. toint32 (configurationsettings. receivettings ["smtpserverport"]);
Username = configurationsettings. deleettings ["username"];
Password = configurationsettings. deleettings ["password"];
// From = configurationsettings. appsettings ["from"];
// Fromname = configurationsettings. receivettings ["fromname"];
Charset = configurationsettings. appsettings ["charset"];
}
Else
{
Smtpservername = ""; // your SMTP Server
Smtpserverport = 25;
Username = ""; // your name
Password = ""; // your pass
Charset = "gb2312 ";
// From = configurationsettings. appsettings ["from"];
// Fromname = configurationsettings. receivettings ["fromname"];
}
}
Catch
{
Return false;
}
Priority = "normal ";
// Subject = "//";
// Smtpcodeadd ();
Return true;
}
Private string base64encode (string Str)
{
Byte [] barray;
Barray = encoding. Default. getbytes (STR );
Return convert. tobase64string (barray );
}
Private void smtpcodeadd ()
{
Rightcodeht. Add ("220 ","");
Rightcodeht. Add ("250 ","");
Rightcodeht. Add ("251 ","");
Rightcodeht. Add ("354 ","");
Rightcodeht. Add ("221 ","");
Rightcodeht. Add ("334 ","");
Rightcodeht. Add ("235 ","");
}
Private bool sendcommand (string Str)
{
Byte [] writebuffer;
Writebuffer = encoding. Default. getbytes (STR );
Try
{
Networkstm. Write (writebuffer, 0, writebuffer. Length );
}
Catch
{
Return false;
}
Return true;
}
Private bool isright ()
{
Int streamsize;
Byte [] readbuffer = new byte [1024];
String returnvalue = "";
Try
{
Streamsize = networkstm. Read (readbuffer, 0, readbuffer. Length );
}
Catch
{
Return false;
}
If (streamsize! = 0)
Returnvalue = encoding. Default. getstring (readbuffer, 0, streamsize );
If (rightcodeht [returnvalue. substring (0, 3)] = NULL)
Return false;
Return true;
}
Public bool Sendmail ()
{
If (! Initialize ())
Return false;
Try
{
Tcpclt = new tcpclient (smtpservername, smtpserverport );
}
Catch
{
Return false;
}
Networkstm = tcpclt. getstream ();
If (! Isright ())
Return false;
String [] sendbuffer;
String enter = "/R/N ";
Sendbuffer = new string [9];
Sendbuffer [0] = "ehlo" + smtpservername + enter;
Sendbuffer [1] = "auth login" + enter;
Sendbuffer [2] = base64encode (username) + enter;
Sendbuffer [3] = base64encode (password) + enter;
Sendbuffer [4] = "mail from: <" + from + ">" + enter;
Sendbuffer [5] = "rcpt to: <" + to + ">" + enter;
Sendbuffer [6] = "data" + enter;
Sendbuffer [7] = "from:" + fromname + "<" + from + ">" + enter;
Sendbuffer [7] + = "to: =? "+ Charset. toupper () + "? B? "
+ Base64encode (recipientname) + "? = "+" <"+ To +"> "+ enter;
Sendbuffer [7] + = "Subject:" + "=? "+ Charset. toupper () + "? B? "
+ Base64encode (subject) + "? = "+ Enter;
Sendbuffer [7] + = "X-priority:" + priority + enter;
Sendbuffer [7] + = "X-msmail-priority:" + priority + enter;
Sendbuffer [7] + = "importance:" + priority + enter;
Sendbuffer [7] + = "X-mailer: huolx. pubclass" + enter;
Sendbuffer [7] + = "mime-version: 1.0" + enter;
Sendbuffer [7] + = "Content-Type: multipart/mixed;" + enter;
Sendbuffer [7] + = "boundary =/" ---- = _ nextpart_000_00d6_01c29593.aab31770/"" + enter;
Sendbuffer [7] + = "------ = _ nextpart_000_00d6_01c29593.aab31770" + enter;
Sendbuffer [7] + = "Content-Type: text/html;" + enter;
Sendbuffer [7] + = "charset =/" "+ charset. tolower () +"/"" + enter;
Sendbuffer [7] + = "content-transfer-encoding: base64" + enter;
Sendbuffer [7] + = base64encode (body) + enter;
Sendbuffer [7] + = "------ = _ nextpart_000_00d6_01c29593.aab31770 --" + enter;
Sendbuffer [7] + = enter + "." + enter;
Sendbuffer [8] = "quit" + enter;
Int I;
For (I = 0; I <sendbuffer. length; I ++)
{
If (! Sendcommand (sendbuffer [I])
Return false;
If (! Isright ())
Return false;
}
Tcpclt. Close ();
Networkstm. Close ();
Return true;
}
Public int send_email (string from, string to, string fromname, string toname, string subject, string body)
{
Int issuccess = 0;
String S1 =;
Int IX;
Int Iy;
Int iz;
Char split;
Split = ',';
String [] mailaddress;
IX = to. lastindexof ("@");
Iy = to. lastindexof (".");
Iz = to. lastindexof (",");
If (IX> 0 & Iy> 0 & Iy> IX)
{
If (IZ> 0)
{
Mailaddress = s1.split (split );
For (INT I = 0; I <mailaddress. length; I ++)
{
IX = mailaddress [I]. lastindexof ("@");
If (mailaddress [I]. substring (IX + 1) = "Sina.com ")
{Send_method = "1 ";}
Else {send_method = "0 ";}
Msendmail mysendmail = new msendmail (toname, mailaddress [I], fromname, from, subject, body );
Try
{
If (mysendmail. Sendmail () = true)
Issuccess = 0;
}
Catch
{
}
}
}
Else
{
If (s1.substring (IX + 1) = "Sina.com ")
{Send_method = "1 ";}
Else {send_method = "0 ";}
Msendmail mysendmail = new msendmail (toname, to, fromname, from, subject, body );
Try
{
If (mysendmail. Sendmail () = true)
Issuccess = 0;
}
Catch
{}
}
}
Else {issuccess = 2 ;}
Return issuccess;
}
Public int send_tuijian (string from, string to, string fromname, string toname, String title, string newsaddr, string message)
{
// Read the email content
String messagepath;
If (system. configuration. configurationsettings. receivettings ["messagepath"]! = NULL)
Messagepath = system. configuration. configurationsettings. receivettings ["messagepath"]. tostring ();
Else
Messagepath = @ "D:/abc.htm ";
String strtemplate;
Streamreader stream = new streamreader (messagepath, system. Text. encoding. getencoding ("gb2312 "));
Try
{
Stream. basestream. Seek (0, seekorigin. Begin );
Strtemplate = stream. readtoend ();
Strtemplate. Replace ("/"","'");
}
Finally
{
Stream. Close ();
}
// Replace
String tmpmessage = message;
Try
{
For (INT I = 0; I <= (message. Length/35); I ++)
{
Tmpmessage = tmpmessage. insert (I + 1) * 35, "<br> ");
}
}
Catch
{
}
Message = tmpmessage;
Message = message + "<br> ";
Strtemplate = strtemplate. insert (strtemplate. lastindexof (", "), message );
Strtemplate = strtemplate. Replace ("AA", toname );
Strtemplate = strtemplate. Replace ("BB", fromname );
Strtemplate = strtemplate. Replace ("cc", title );
Strtemplate = strtemplate. Replace (@ "DD", newsaddr );
Strtemplate = strtemplate. Replace ("1980", datetime. Now. tow.datestring ());
// Send an email
Int issuccess = 0;
String subject = "would you like to see it ";
// Email address judgment
String S1 =;
Int IX;
Int Iy;
Int iz;
Char split;
Split = ',';
String [] mailaddress;
IX = to. lastindexof ("@");
Iy = to. lastindexof (".");
Iz = to. lastindexof (",");
If (IX> 0 & Iy> 0 & Iy> IX)
{
If (IZ> 0)
{
Mailaddress = s1.split (split );
For (INT I = 0; I <mailaddress. length; I ++)
{
IX = mailaddress [I]. lastindexof ("@");
If (mailaddress [I]. substring (IX + 1) = "Sina.com ")
{Send_method = "1 ";}
Else {send_method = "0 ";}
Msendmail mysendmail = new msendmail (toname, mailaddress [I], fromname, from, subject, strtemplate );
Try
{
If (mysendmail. Sendmail () = true)
Issuccess = 0;
}
Catch
{
}
}
}
Else
{
If (s1.substring (IX + 1) = "Sina.com ")
{Send_method = "1 ";}
Else {send_method = "0 ";}
Msendmail mysendmail = new msendmail (toname, to, fromname, from, subject, strtemplate );
Try
{
If (mysendmail. Sendmail () = true)
Issuccess = 0;
}
Catch
{}
}
}
Else {issuccess = 2 ;}
Return issuccess;
}
}
}