国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

一個發郵件的例子,涉及MSMQ,RSA,JMAIL

2024-07-21 02:24:36
字體:
來源:轉載
供稿:網友
中國最大的web開發資源網站及技術社區,
 

1.先生成公鑰密鑰

   rsacryptoserviceprovider crypt=new rsacryptoserviceprovider();
   string publickey=crypt.toxmlstring(false);//(公鑰)
   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("成功保存公匙和密匙!");

2.對信息加密,然后用通過隊列發送信息

   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 );//讀取公鑰
   bytes = crypt.encrypt( bytes,false ); //進行加密
   string encryttext=convert.tobase64string(bytes); //轉碼
   
   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.在服務器端獨立運行程序,在隊列里面讀取信息

 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;
    //解碼

    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="[email protected]";


   }
   catch (messagequeueexception)
   {
   }
   catch (invalidoperationexception e)
   {
    console.writeline(e.message);
   }
   
   //發送郵件

   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";
   }
   //開始發送郵件
   int i=0;
   try
   {
    jmail.send(smtp,false);
   }
   catch(exception ee)
   {
    i=1;
   }
   jmail.close() ;

   if(i==0)
      console.writeline("郵件發送成功"+"發送人:"+from+"接收方:"+to+"主題是:"+sub);
   if(i==1)
    console.writeline("登陸失敗,或者網絡故障");

   
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻江县| 敦化市| 石城县| 秦安县| 万荣县| 阳谷县| 澜沧| 东山县| 马尔康县| 白玉县| 易门县| 中西区| 临汾市| 静海县| 司法| 江安县| 泰顺县| 道孚县| 门源| 河北省| 通许县| 抚州市| 中卫市| 田阳县| 策勒县| 北宁市| 拉孜县| 武清区| 贺兰县| 新和县| 米易县| 丹巴县| 蕲春县| 敦化市| 渭源县| 吴川市| 柳河县| 保靖县| 边坝县| 和田县| 龙川县|