ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ASP.NET2.0发送电子邮件示例代码

ASP.NET2.0发送电子邮件示例代码

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-12 16:33:52 0 删除 编辑

下面的代码是我参考资料写出的一个示例:

 

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

namespace Email_Test.aspx
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string emailTitle = "测试email";
            string toEmail = "seagreen7@yeah.net";
            string mycontent = "这是测试邮件内容";
            string myResult = SendHtmlEmail(emailTitle, toEmail, mycontent);
            if (myResult == "ok")
            { this.Label1.Text = "恭喜,邮件已经成功发送给" + toEmail; }
            else
            { this.Label1.Text = "抱歉,邮件发送失败,请检查web.config文件的配置信息 system.net 节点。" ; }
        }
        public static string SendHtmlEmail(string EmailTitle, string destEmail, string EmailContent)
        {
            try
            {
                // 读取web.config中的邮件发送的配置信息
                //在这里的代码中,我们不需要设置SmtpClient类的任何属性,因为它们已经在Web.config文件中指定了

                //create the mail message
                MailMessage mail = new MailMessage();

                //set the addresses
                mail.To.Add(destEmail);

                //set the content
                mail.Subject = EmailTitle;

                //screen scrape the html
                string html = EmailContent;
                mail.Body = html;
                mail.IsBodyHtml = true;

                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.Send(mail);
            }
            catch (Exception e)
            {
                return "fail
" + e.ToString(); //发送失败,返回fail
            }
            return "ok"; //发送成功,返回 ok

        }
    }
}

 

 

web.config的内容如下:

 


   
      <!-- 发送邮件设置,把这里的邮箱地址和密码设置成你自己的就ok了 -->
      yours@126.com">
        yours@126.com" password="123456" defaultCredentials="false"/>
     

   

 


 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-536503/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5322106