asp.net 发送邮件

1.

 

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 LTP.Accounts.Bus;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;
using System.Web.Mail;
using System.Text.RegularExpressions;

namespace Studeny.Common
{
   public  class SendMail
    {

        public static bool Send(string strTo, string strBody, string strSubject, string strForm, string strUserName, string strPwd, string strServer)
        {

            MailMessage mm = new MailMessage();//创建邮件对象

            mm.From = strForm;//获取发件人的电子邮箱(多人用","分隔)

            mm.To = strTo;//获取收件人的电子邮箱

            mm.Subject = strSubject;//邮件标题

            mm.Body = strBody;//邮件正文

            mm.BodyFormat = System.Web.Mail.MailFormat.Html;//格式

            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//基本权限

            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strUserName);//自己的邮箱名

            mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPwd);//自己的邮箱密码

            SmtpMail.SmtpServer = strServer;//服务器地址

            try
            {

                SmtpMail.Send(mm);

                return true;

            }

            catch
            {

                return false;

            }

        }

        public static bool isEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
            {
                return true;
            }
            else
            {
                return false;
            }
        }  
    }
}

 

 

 

2.调用

  if (Common.SendMail.Send(lblUserName.Text.Trim(), "内容", "我要听课", ConfigurationManager.AppSettings.Get("WebEmail"), ConfigurationManager.AppSettings.Get("WebEmailUserName"), ConfigurationManager.AppSettings.Get("WebEmailpwd"), ConfigurationManager.AppSettings.Get("WebEmailServer")))
                            {
                                Common.MessageBox.Show(this.Page, "恭喜您!邮件发送成功");
                            }
                            else
                            {
                                Common.MessageBox.Show(this.Page, "SORRY!邮件发送失败");
                            }
                            return;

 

3.web.config

 

 <appSettings>

  <add key="WebEmailServer" value="smtp.163.com"/>
  <!-- 发件邮箱-->
  <add key="WebEmail" value="www@163.com"/>
  <!-- 发件邮箱用户名-->
  <add key="WebEmailUserName" value="www"/>
  <!-- 发件邮箱密码-->
  <add key="WebEmailpwd" value="www"/>

</appSettings>

 

 

posted on 2012-04-17 22:49  魏绰  阅读(344)  评论(0编辑  收藏  举报

导航