1. Generate a public key.
Rsacryptoserviceprovider crypt = new rsacryptoserviceprovider ();
String publickey = crypt. toxmlstring (false); // (Public Key)
String privatekey = crypt. toxmlstring (true );
Crypt. Clear ();
Streamwriter one = new streamwriter (@ "C:/a.txt", true, utf8encoding. utf8 );
One. Write (publickey );
Streamwriter two = new streamwriter (@ "C:/B .txt", true, utf8encoding. utf8 );
Two. Write (privatekey );
One. Flush ();
Two. Flush ();
One. Close ();
Two. Close ();
Console. writeline ("the public key and secret key are successfully saved! ");
2. encrypt the information and send the information through the queue.
String from = TextBoxFrom. Text + DropDownList2.SelectedValue;
String sub = textBoxSub. Text;
String bodys = TextBoxBody. Text;
String pwd = TextBoxPwd. Text;
StreamReader sr = new StreamReader (@ "c:/a.txt", UTF8Encoding. UTF8 );
String readpublickey = sr. ReadToEnd ();
Sr. Close ();
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider ();
UTF8Encoding enc = new UTF8Encoding ();
Byte [] bytes = enc. GetBytes (pwd );
Crypt. FromXmlString (readpublickey); // read the Public Key
Bytes = crypt. Encrypt (bytes, false); // Encrypt
String encryttext = Convert. ToBase64String (bytes); // transcode
Mailerinfo mf = new mailerinfo ();
// MF. Body = bodys;
Mf. Body = bodys;
Mf. From = from;
Mf. fromname = textboxfrom. text;
Mf. Password = encryttext;
Mf. sub = sub;
// Createqueue (". // myqueue ");
Sendmessage (MF );
}
Public static void createqueue (string queuepath)
{
Try
{
If (! Messagequeue. exists (queuepath ))
{
Messagequeue. Create (@ "./private $/myqueue ");
}
Else
{
Console. writeline (queuepath + "already exists .");
}
}
Catch (messagequeueexception E)
{
Console. writeline (E. Message );
}
}
Public void sendmessage (mailerinfo MF)
{
Try
{
Messagequeue myqueue = new messagequeue (". // Private $ // myqueue ");
System. messaging. Message mymessage = new system. messaging. Message (MF );
Myqueue. Send (mymessage );
}
Catch (argumentexception E)
{
Console. writeline (E. Message );
}
Return;
}
3. Run the program on the server and read the information in the queue.
Public void receivemessage ()
{
Messagequeue myqueue = new messagequeue (". // Private $ // myqueue ");
Myqueue. formatter = new xmlmessageformatter (New Type [] {typeof (messagerec. mailerinfo )});
Try
{
System. messaging. Message mymessage = myqueue. Receive ();
Mailerinfo mf = (mailerinfo) mymessage. Body;
// Decoding
Streamreader sr = new streamreader (@ "C:/B .txt", utf8encoding. utf8 );
String readprivatekey = Sr. readtoend ();
Sr. Close ();
Rsacryptoserviceprovider crypt = new rsacryptoserviceprovider ();
Utf8encoding ENC = new utf8encoding ();
Byte [] bytes = convert. frombase64string (MF. Password );
Crypt. fromxmlstring (readprivatekey );
Byte [] decryptbyte = crypt. decrypt (bytes, false );
Password = enc. getstring (decryptbyte );
From = mf. From;
Fromname = mf. fromname;
Sub = mf. sub;
Body = mf. Body;
To = "dankes@163.com ";
}
Catch (messagequeueexception)
{
}
Catch (invalidoperationexception E)
{
Console. writeline (E. Message );
}
// Send an email
Jmail. Message jmail = new jmail. Message ();
Jmail. Silent = false;
Jmail. Logging = true;
Jmail. charset = "gb2312 ";
Jmail. contenttype = "text/html ";
Jmail. addrecipient (,"","");
Jmail. From = from;
Jmail. mailserverusername = fromname;
Jmail. mailserverpassword = password;
Jmail. Subject = sub;
Jmail. Body = body;
String SMTP = "smtp.163.com ";
If (from. endswith ("tom.com "))
{
SMTP = "smtp.tom.com ";
}
Else if (from. endswith ("21cn.com "))
{
SMTP = "smtp.21cn.com ";
}
Else if (from. endswith ("Sina.com "))
{
SMTP = "smtp.sina.com ";
}
Else if (from. endswith ("263.com "))
{
SMTP = "smtp.263.com ";
}
// Start sending emails
Int I = 0;
Try
{
Jmail. Send (SMTP, false );
}
Catch (exception ee)
{
I = 1;
}
Jmail. Close ();
If (I = 0)
Console. writeline ("email sent successfully" + "Sender:" + from + "receiver:" + to + "Subject:" + sub );
If (I = 1)
Console. writeline ("Logon failed or network fault ");
}