用Jmail实现邮件发送源代码
以下代码经过测试,没有问题的,可以实现邮件发送。
JMail下载地址:http://www.newhua.com/soft/1027.htm
记得在使用Jmail的时候要添加引用。
using System;
using System.Data;
using System.Configuration;
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 System.Web.Mail;
using System.Web.Util;
using jmail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
jmail.Message Jmail = new jmail.MessageClass();
DateTime t = DateTime.Now;
String Subject = this.txtSubject.Text;
String Body = this.txtBody.Text;
String FromEmail = this.txtFromEmail.Text;
String ToEmail = this.txtToEmail.Text;
//Silent属性:如果设置为true,JMail不会抛出例外错误.
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 = this.txtUserName.Text;
//发件人邮件密码
Jmail.MailServerPassWord = this.txtPassword.Text;
//设置邮件标题
Jmail.Subject = Subject;
////邮件添加附件(增加附件可再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);就可以搞定了。
////增加附件后要把上面的Jmail.ContentType="text/html";删掉,否则会在邮件里出现乱码。
//Jmail.AddAttachment("c:\\test.jpg", true, null);
//邮件内容
Jmail.Body = Body+t.ToString();
//设置SMTP服务器
Jmail.Send("smtp.126.com", false);
//关闭Jmail连接
Jmail.Close();
Response.Write("发送成功");
}
}
using System.Data;
using System.Configuration;
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 System.Web.Mail;
using System.Web.Util;
using jmail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
jmail.Message Jmail = new jmail.MessageClass();
DateTime t = DateTime.Now;
String Subject = this.txtSubject.Text;
String Body = this.txtBody.Text;
String FromEmail = this.txtFromEmail.Text;
String ToEmail = this.txtToEmail.Text;
//Silent属性:如果设置为true,JMail不会抛出例外错误.
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 = this.txtUserName.Text;
//发件人邮件密码
Jmail.MailServerPassWord = this.txtPassword.Text;
//设置邮件标题
Jmail.Subject = Subject;
////邮件添加附件(增加附件可再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);就可以搞定了。
////增加附件后要把上面的Jmail.ContentType="text/html";删掉,否则会在邮件里出现乱码。
//Jmail.AddAttachment("c:\\test.jpg", true, null);
//邮件内容
Jmail.Body = Body+t.ToString();
//设置SMTP服务器
Jmail.Send("smtp.126.com", false);
//关闭Jmail连接
Jmail.Close();
Response.Write("发送成功");
}
}