小二哥's blog

----zhangzs8896(小二)

导航

在.net中使用jmail发送Email(部分代码)

Posted on 2004-12-06 19:29  小二哥  阅读(813)  评论(0编辑  收藏  举报
1安装jmail4.3
2.找到jmail.dll (E:\ProgramFiles\JMail4下)
3.执行Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe(可使用Visual Studio .Net 2003 命令提示),
格式如下:tlbimp E:\ProgramFiles\JMail4\jmail.dll /out:myJmail.dll /namespace:pu1_erp
生成myJmail.dll后,copy到web的根目录的bin目录。
4.使用时先加myJmail.dll引用

部分代码:
Jmail_Send.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using pu1_erp; //加它,原因是生成myjmail.dll时
using System.IO;

namespace Pu1_Erp.Email

  public class Jmail_Send : System.Web.UI.Page
 { 
  ...................;
  string smtp; //加一个全局变量 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Label7.Text="'用户名'与'密码'是相对发邮件者而言的"; 
  }

  public void send()
  { 
   try
  {
  pu1_erp.MessageClass Jmail=new pu1_erp.MessageClass() ;
  Jmail.Silent = true ;
  Jmail.FromName =user_name.Text.Trim();    
  Jmail.From = user_name.Text.Trim()+mail_ddlst.SelectedItem.Value.Trim() ;    
  Jmail.MailServerUserName =user_name.Text.Trim() ;     
  Jmail.MailServerPassWord =user_pwd.Text.Trim(); 
  Jmail.Priority=3;
  Jmail.Subject =Subject_txt.Text ;
  // 邮件内容的空格、回车等替换
  string strNews;
  strNews=Body_txt.Text;
  strNews=strNews.Replace("\r","<br>");
  strNews=strNews.Replace("\0","&nbsp;&nbsp;");
  strNews=strNews.Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
  strNews=strNews.Replace(" ","&nbsp;&nbsp;");
  //
  Jmail.HTMLBody=strNews ;
  //Jmail.ContentType ="text/html" ;  //若发送附件则删除本句
  Jmail.Charset ="gb2312" ;      
  Jmail.Encoding ="base64" ;
  Jmail.ISOEncodeHeaders = false;
  
  string file;
  file=filepath_txt.Text.Trim(); 
  if(file!="")
  {
   Jmail.AddAttachment(file,true,null);//
  }
  string mail;
  mail=recipient.Text.Trim();
  Jmail.AddRecipient(mail,"","") ;
   switch(mail_ddlst.SelectedItem.Text.Trim())
   {
    case "TPCO": 
       smtp="dns.tpco.com.cn";
       break;
    case "163信箱":
       smtp="smtp.163.com";
       break;
    case  "新浪100":
      smtp="smtp.sina100.com";
      break;
    
   }
  Jmail.Send(smtp,false);
  ok_error.Text="Ok! 发件人:"+user_name.Text.Trim()+mail_ddlst.SelectedItem.Value.Trim();
  ok_error.ForeColor=Color.Green;
   }
  catch
  {
    ok_error.Text="Error!";
    ok_error.ForeColor=Color.Red;
  }
   ok_error.Visible=true;
 }

  private void Send_mail_Click(object sender, System.EventArgs e)
  {
   if(recipient.Text!=""){
    send();
   }
   else{
    ok_error.Text="请写好'收件人'地址!";
    ok_error.ForeColor=Color.Red;
    ok_error.Visible=true;
   }
    recipient.Text="";
    Subject_txt.Text="";
    Body_txt.Text="";
    filepath_txt.Text=""; 
  }

  private void Attachment_Click(object sender, System.EventArgs e)
  {
   Label6.Visible=true; 
   //filepath_txt.Text="c:\\test.jpg"; 
   filepath_txt.Text=File1.PostedFile.FileName.ToString(); 
   
  }

  private void Reset_write_Click(object sender, System.EventArgs e)
  {
   Body_txt.Text="";
   //Body_txt.set
  }

 }
}
为了可以打开文件浏览框在html中加:

<INPUT id="File1" style="Z-INDEX: 116; LEFT: 88px; WIDTH: 272px; BORDER-TOP-STYLE: inset; BORDER-RIGHT-STYLE: inset; BORDER-LEFT-STYLE: inset; POSITION: absolute; TOP: 504px; HEIGHT: 22px; BORDER-BOTTOM-STYLE: inset" accessKey="" type="file" size="26" name="Myfile" runat="server">


图片:http://www.cnblogs.com/zhangzs8896/gallery/image/7831.html