asp.net发送邮件代码(Gmail、163能发)

  1. 云栖社区>
  2. 博客>
  3. 正文

asp.net发送邮件代码(Gmail、163能发)

真爱无限 2012-06-23 14:01:04 浏览2111
展开阅读全文
    今天测试了gmail的邮箱能发出去,其它的我测试是发不出去。

  #region 发送邮件的函数
        /// <summary>
        /// 发送邮件函数Gmail
        /// </summary>
        /// <param name="EmailSubject">邮件主题</param>
        /// <param name="EmailBody">邮件内容</param>
        /// <param name="smtpserver">邮箱服务器(为空值则为smtp.gmail.com)</param>
        /// <param name="receiveEmail">接收邮箱</param>
        /// <param name="FromEmail">发送者邮箱</param>
        /// <param name="FromPassword">发送者邮箱密码</param>
        /// <param name="FromEmailName">显示发送者邮箱名字</param>
        /// <param name="SmtpPort">发送邮件的端口(默认为 587)</param>
        /// <returns>返回提示信息</returns>
        public static string SendEmailG(string EmailSubject, string EmailBody, string smtpserver, string receiveEmail, string FromEmail, string FromPassword, string FromEmailName,params int[]SmtpPort)
        {
            try
            {
                FromEmailName=(FromEmailName==""?"系统发出":FromEmailName);
                smtpserver = (smtpserver == "" ? "smtp.gmail.com" : smtpserver);
                System.Net.Mail.MailMessage mailObj = new System.Net.Mail.MailMessage();
                mailObj.IsBodyHtml = true;
                mailObj.Subject = EmailSubject;
                mailObj.Body = EmailBody;
                mailObj.To.Add(new MailAddress(receiveEmail, receiveEmail, System.Text.Encoding.UTF8));
                mailObj.BodyEncoding = System.Text.Encoding.UTF8;
                mailObj.SubjectEncoding=System.Text.Encoding.UTF8;
                System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(smtpserver);
                mailObj.From = new MailAddress(FromEmail, FromEmailName, System.Text.Encoding.UTF8);
                SmtpMail.Credentials = new System.Net.NetworkCredential(FromEmail, FromPassword);
                //gmail 专有配置 开始
                SmtpMail.Port =(SmtpPort.Length<=0?587:SmtpPort[0]);
                SmtpMail.EnableSsl = true;
                //gmail 专有配置 结束
                SmtpMail.Send(mailObj);
                return "发送成功!";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        #endregion

 

//163邮箱的能发

        string ErrMsg = "";

        public Email()
        {

        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="smtpserver">邮件服务器</param>
        /// <param name="mailuser">登录帐号</param>
        /// <param name="mailpassword">登录密码</param>
        /// <param name="mailFrom">发送人</param>
        /// <param name="mailContent">邮件内容</param>
        /// <param name="mailTo">收信邮箱</param>
        /// <param name="mailTitle">邮件标题</param>
        /// <returns></returns>
        public bool SendEmail(string smtpserver, string mailuser, string mailpassword, string mailFrom, string mailContent, string mailTo, string mailTitle)
        {
            MailMessage mail = new MailMessage(mailFrom, mailTo);
            mail.BodyEncoding = Encoding.Default;
            mail.Subject = mailTitle;
            mail.Body = mailContent;
            mail.Priority = MailPriority.Normal;
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(smtpserver);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(mailuser, mailpassword);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                smtp.Send(mail);
                mail.Dispose();//释放对象
                smtp.Dispose();//释放对象
            }
            catch (Exception ex)
            {
                ErrMsg = ex.Message;
                return false;
            }
            return true;
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="config">配置文件config对象</param>
        /// <param name="mailContent">邮件内容</param>
        /// <param name="mailTo">收信邮箱</param>
        /// <param name="mailTitle">邮件标题</param>
        /// <returns></returns>
        public bool SendEmail(DataRow config, string mailContent, string mailTo, string mailTitle)
        {
            string MailSmtpServer = config["MailSmtpServer"].ToString();
            string MailServerUserName = config["MailServerUserName"].ToString();
            string MailServerPassWord = config["MailServerPassWord"].ToString();
            string MailServerFrom = config["MailServerFrom"].ToString();
            return SendEmail(MailSmtpServer, MailServerUserName, MailServerPassWord, MailServerFrom, mailContent, mailTo, mailTitle);
        }
        //<MailSmtpServer>smtp.163.com</MailSmtpServer>
        //<MailServerUserName>test@163.com</MailServerUserName>
        //<MailServerPassWord>*****</MailServerPassWord>
        //<MailServerFrom>test@163.com</MailServerFrom>




-----pukuimin

网友评论

登录后评论
0/500
评论