C# 邮件发送方法【webMail方式】(2)

  //这里的邮箱帐号和密码一定要和下面配置文件中设置的邮箱的帐号和密码一致
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxxxxx");//邮箱帐号,比如Test11@126.com帐号为:Test11
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxxxx");//邮箱密码
            //这个是指明邮件服务器的端口,可以不指定
            //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25");
     

            foreach (string str in strTemp.Split(';'))
            {
                if (reg.IsMatch(str))
                {
                    message.To = str;
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    System.Web.Mail.SmtpMail.SmtpServer = strSMTPServer;
                   
                    System.Web.Mail.SmtpMail.Send(message);
                }
            }

            bState = true;
        }
        catch (Exception ex)
        {
            System.IO.File.AppendAllText("C:\\Mail_Log.ini", string.Format("{0:yyyy/MM/dd HH:mm:ss}\r\n{1}\r\n\r\n", DateTime.Now, ex.Message));
            bState = false;
            strErrorMsg = ex.Message;
        }

        return bState;
    }

//测试发送邮件

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
           
            Email.SendEmail("xxxxxx@163.com", "", "Test Email", "Test Send Email");
         
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

}

邮件在webconfig文件中配置如下:

C# 邮件发送方法【webMail方式】

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wydpdx.html