C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

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;
namespace BaseProject.Classes.Util
{
    public class MailManage
    {
        public bool SendMail(string toMail, string subject, string body)
        {
            try
            {
                string fromMail = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["mailService"] ?? "");
                MailMessage myMail = new MailMessage();
                myMail.From = fromMail;
                myMail.To = toMail;

                myMail.Subject = subject;
                myMail.Body = body;
                myMail.BodyFormat = MailFormat.Html;

                ////附件
                //string ServerFileName = "";
                //if (this.upfile.PostedFile.ContentLength != 0)
                //{
                //    string upFileName = this.upfile.PostedFile.FileName;
                //    string[] strTemp = upFileName.Split('.');
                //    string upFileExp = strTemp[strTemp.Length - 1].ToString();
                //    ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
                //    this.upfile.PostedFile.SaveAs(ServerFileName);
                //    myMail.Attachments.Add(new MailAttachment(ServerFileName));
                //}


                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["mailUser"] ?? "")); //发送方邮件帐户
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["mailPass"] ?? "")); //发送方邮件密码

                SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
                SmtpMail.Send(myMail);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

posted on 2009-04-02 18:35  王德田  阅读(197)  评论(0编辑  收藏  举报