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);
}
}
}
}
引发的异常:“System.Web.HttpException”(位于 System.Web.dll 中)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2020-10-29 14:18关注
可能是465端口被禁用了,尤其是你的服务器是国内机房/云服务器的话,或者服务商用的是587/993等别的端口。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改