邮件发送控件jmail使用方法之ASP.NET示例

来源:互联网 发布:淘宝彩虹系统 编辑:程序博客网 时间:2024/05/21 22:26

    准备工作和WinForm程序中应用的差不多:

 

    1.首先下载jmail控件,然后安装(其实不安装也行,只要你能找到jmail.dll文件,然后注册该dll文件。这里不支持上传附件,否则我就把我下载的传上来了),附上手工注册它的批处理:

echo off
copy jmail.dll C:/windows/system32
regsvr32 jmail.dll
echo 执行完毕!
pause

 

    2.使用tlbimp c:/Program Files/Dimac/w3JMail4/jmail.dll /out:myJmail.dll /namespace:myJmail生成myJmail.dll后,copy到web的根目录的bin目录。在ASP.Net页面中,用using myjmail;方法引用,例程如下:

 

protected void Page_Load(object sender, EventArgs e)
{
   myjmail.Message jmail = new myjmail.Message();

   DateTime t=DateTime.Now;  
   string subject = "jmail test from web";  
   string body= "<center>jmail test from web<br>test</center>";  //tbContent.Text.Replace("/n","<br>");
   string fromemail="xxxx@e165.com";  
   string toEmail= "xxxx@e165.com";
   //silent属性:如果设置为true,jmail不会抛出例外错误. jmail. send( () 会根据操作结果返回true或false
   jmail.Silent = true;
   //jmail创建的日志,前提loging属性设置为true
   //jmail.Logging=true;
   //字符集,缺省为"us-ascii"
   jmail.Charset="gb2312";
   //信件的contentype. 缺省是"text/plain") : 字符串如果你以html格式发送邮件, 改为"text/html"即可。
   jmail.ContentType="text/html";
   //添加收件人
   jmail.AddRecipient(toEmail,"","");
   jmail.From = fromemail;
   //发件人邮件用户名
   jmail.MailServerUserName="xxxx" ;
   //发件人邮件密码
   jmail.MailServerPassWord="xxxx" ;
   //设置邮件标题
   jmail.Subject=subject;
   //邮件添加附件,(多附件的话,可以再加一条jmail.addattachment( "c://test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的jmail.contenttype="text/html";删掉。否则会在邮件里出现乱码。
   //jmail.addattachment( "c://test.jpg",true,null);
   //邮件内容
   jmail.Body=body;
   //jmail发送的方法

   if(jmail.Send("smtp.e165.com",false))
       lbResult.Text = "已成功发送邮件。";
   else
       lbResult.Text = "发送邮件失败!!!";
    
   jmail.Close() ;