.NET2.0示例_邮件发送

利用MailMessage对象

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 System.Net.Mail;
using System.Net;

public partial class sendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_sendmail_Click(object sender, EventArgs e)
    {
        //设置from和to地址
        MailAddress from = new MailAddress(Request["txtFrom"],Request["txtFromer"]);
        MailAddress to = new MailAddress(Request["txtTo"], Request["txtToer"]);
       
        //创建一个MailMessage对象
        MailMessage oMail = new MailMessage(from, to);
       
        //邮件标题
        oMail.Subject = Request["txtTitle"];

        //邮件内容
        oMail.Body = Request["txtContent"];

        //邮件格式
        oMail.IsBodyHtml = true;

        //邮件采用的编码
        oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");

        //设置邮件的优先级为高
        oMail.Priority = MailPriority.High;

        //发送邮件
        SmtpClient client = new SmtpClient();      
        client.Host = "mail.cpp114.net";      
        client.Credentials = new NetworkCredential("yjm@cpp114.net", "*****");
        try
        {
            client.Send(oMail);
            Response.Write("<font color=green>发送成功!</font>");
        }
        catch (Exception ex) {
            Response.Write("<font color=red>发送失败:" + ex.Message.ToString() + "</font>");
        }

        //释放资源
        oMail.Dispose();
       
    }
}


问题:好象只能在同一个系统中发送(比如都在mail.cpp114.net中发送,不能发送到外部mail)

Jmail发信

不同于在ASP中使用Jmail,直接使用 Server.CreateObject("Jmail.Message")就可以了。在.Net环境中,需要进行设置。
1.安装jmail4.4
2.找到jmail.dll(一般就在Jmail的安装目录下)
3.执行Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe(可使用Visual Studio .Net 2003 命令提示),
格式如下:
D:\VS.NET2005\VC>tlbimp "c:\Program Files\JMail\jmail.dll" /out:myJmail.dll /namespace:myJmail
生成myJmail.dll后,copy到web的根目录的bin目录。在ASP.Net CodeBehind代码文件中,用这个方法引用:


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 myJmail;
using emailTools;//这里导入了email.cs的命名空间,详情请参看email.cs

public partial class Jmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_sendmail_Click(object sender, EventArgs e)
    {
        string subject = "Jmail.Net test " + DateTime.Now;
        string smtpAddress = "smtp.163.com";
        string senderName = "神秘来客";
        string receiverName = "亲爱的杨俊明";
        string toAddress = "yangjm@cpp114.net";
        string content = "这是从ASP.NET2.0 Jmail发送过来的信息" + DateTime.Now;
        string fromAddress = "endlesspower@163.com";
        string serEmailUser = "endlesspower";
        string serEmailPass = "********"; 
        email.JmailSend(subject, smtpAddress, senderName, receiverName, toAddress, content, fromAddress, serEmailUser, serEmailPass);

    }

}

posted on 2006-06-26 23:45  Ameng  阅读(435)  评论(0)    收藏  举报