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

來源:互聯網
上載者:User
jmail|發郵件  

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="dankes@163.com";


   }
   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("登陸失敗,或者網路故障");

   
  }



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。