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

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有