我想摘星辰
2020-10-29 11:28
采纳率: 100%
浏览 350

引发的异常:“System.Web.HttpException”(位于 System.Web.dll 中)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //发件人
            const string sendAddress = "serveripchange.com";
            // const string sendAddress = "3461094650@qq.com";
            //发件人密码
            const string sendPassword = "autoview123";
            //const string sendPassword = "xxxxxx";
            //收件人
            const string receiveAddress = "zhaixingchen_0523@163.com";

            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            try
            {
                mail.From = sendAddress;
                mail.To = receiveAddress;
                mail.Subject = "whl测试发送邮件2-26-1";
                mail.BodyFormat = System.Web.Mail.MailFormat.Html;
                mail.Body = "whl发送的测试邮件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sendAddress); //set your username here 发件人邮箱
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendPassword); //set your password here 发件人邮箱密码
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//发送端口号
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//是否启用ssl

                //需要引用组件System.Web
                System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com";
               // System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";
                System.Web.Mail.SmtpMail.Send(mail);

                Console.WriteLine("发送邮件成功");
            }
            catch (Exception ex)
            {
                var exg = ex.InnerException ?? ex;
                string msg = exg.ToString();
                Console.WriteLine(msg);
            }

        }
    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2020-10-29 14:18
    已采纳

    可能是465端口被禁用了,尤其是你的服务器是国内机房/云服务器的话,或者服务商用的是587/993等别的端口。

    点赞 评论

相关推荐 更多相似问题