An example of an email involving msmq,rsa,jmail

Source: Internet
Author: User
Tags crypt flush msmq
jmail| Send mail

1. Mr. 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 ("Save the public key and keys successfully!") ");

2. Encrypt the information and then send the message 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 Public key
bytes = Crypt. Encrypt (Bytes,false); For encryption
String encryttext=convert.tobase64string (bytes); Transfer code

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 side independently, 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 mail

JMail. Message Jmail=new JMail. Message ();
Jmail.silent=false;
Jmail.logging=true;
Jmail.charset= "GB2312";
Jmail.contenttype= "text/html";
Jmail.addrecipient (To, "", "");
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 mail
int i=0;
Try
{
Jmail.send (Smtp,false);
}
catch (Exception ee)
{
I=1;
}
Jmail.close ();

if (i==0)
Console.WriteLine ("Mail sent successfully" + "Sender:" +from+ "Receiver:" +to+ "subject is:" +sub);
if (i==1)
Console.WriteLine ("Landing failure, or network failure");


}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.