频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

一个发邮件的例子,涉及MSMQ,RSA,JMAIL

来源: 作者: 出处:巧巧读书 2006-06-04 进入讨论组

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("登陆失败,或者网络故障");

   
  }

保留地址 http://www.qqread.com/vc/c889116002.html 更多文章 更多内容请看邮件服务器专题专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章