首先,下载jmail组件,可以直接安装;也可以找到jmail.dll,手动注册组件。命令行环境下,到jmail.dll所在目录,运行regsvr32 jmail.dll,C#程序中,行首加上using jmail,如下:(点击btnSend按钮后发送邮件)
using jmail;
private void btnSend_Click(object sender, System.EventArgs e)
{
jmail.Message jmessage=new jmail.MessageClass();
jmessage.Charset="GB2312";
jmessage.From="fromuser@benq.com";
jmessage.FromName= "eric lv" ;
jmessage.ReplyTo="replayto@benq.com";
jmessage.Subject="test email from jmessage";
jmessage.AddRecipient("receiver@benq.com" ,"eric lv","123");
jmessage.Body= "jmail 内容" ;
jmessage.MailServerUserName= "eric lv" ;
jmessage.MailServerPassWord= "user password" ;
jmessage.Send("smtp-server",false) ;
MessageBox.Show("email sent successfully!");
jmessage.Close() ;
}
最近刚好在整理自己的代码·以便review 所以刚好把以前列出来
此文说下用Jmail组件的邮件发送
基于B/S架构的邮件发送,讲下最常用最流行的一种方法,利用jmail 组件,Google搜索下载安装
安装完毕,新建VS2005网站项目,添加引用 浏览
C:\Program Files\Dimac\w3JMail4目录的 jmail.dll 文件夹 确定 (至于如何添加引用 见Baidu)确定
系统会自动创建Bin文件夹 并含有Interop.jmail.dll文件
邮件发送的 MailSender.aspx.cs 代码如下(经测试可以运行,决不发至少在本机不能有BUG的源码)
代码都有详细的注释
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using jmail; //引用
public partial class System_MailSender : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void SendBtn_Click(object sender, EventArgs e)
...{
string RecieveAddress = Addresssee.Text; //收件者Email
//string MyAddress = PostMail.Text;
string EmailTheme = theme.Text; //邮件主题
string Content = EmailContent.Text; //邮件内容
//-------------------------------------------------------------------------------------------------
Message jmailobj = new Message();
jmailobj.Logging = true;
jmailobj.Silent = true;
jmailobj.MailServerUserName = "Test"; //发信邮件服务器的帐号 因为我公司的邮件没有设密码
jmailobj.MailServerPassWord = "123"; //密码
//例如 163的邮箱 有户名 为 Test 密码123
jmailobj.Body = Content; //上面定义的EmailContent
jmailobj.Charset = "gb2312";
jmailobj.Subject = EmailTheme;
jmailobj.From = "自个的Email"; //例如Eric@163.com
jmailobj.FromName = "邮件测试";
jmailobj.AddRecipient(RecieveAddress, "Name", "A"); //收件人地址
//jmailobj.AddRecipientBCC("test@hotmail.com","ok"); //抄送,一般用不到
//jmailobj.AddRecipientBCC("test@sohu.com.cn","ok");
jmailobj.Priority = 1;
if (jmailobj.Send("shex01.sh.deltagroup.com", false)) //shex01.sh.deltagroup.com 发送邮件服务器
...{
Label1.Text = "Send to Success";
}
else
...{
Label1.Text = "Send to faild!";
}
}
}