邮件发送类代码

邮件发送类代码

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;
            }
        }
    }
}

你可能感兴趣的:(邮件发送类代码)