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("登陆失败,或者网络故障");
}
新闻热点
疑难解答