asp.net mvc4 SMTP协议发送邮件


控制器代码:     

public stringSendEmail()

        {

                System.Net.Mail.SmtpClient client= new System.Net.Mail.SmtpClient();

               //这里使用sina 的邮箱来发送测试,如果是其它邮箱,请根据其它邮箱POP3/IMAP/SMTP服务来设置

 

               client.Host = "smtp.sina.com";

               client.UseDefaultCredentials = false;

               client.Credentials = newSystem.Net.NetworkCredential("[email protected]", "@zhang1230");//发送方的邮箱账号,密码

               client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

               System.Net.Mail.MailMessage message= new System.Net.Mail.MailMessage();

               message.From = newSystem.Net.Mail.MailAddress("[email protected]");

               message.To.Add("[email protected]");//接收邮箱地址

                message.Subject = "主题"//题目

               message.Body = "内容";   //内容

               message.BodyEncoding = System.Text.Encoding.UTF8;  //编码方式

               message.IsBodyHtml = true;

 

               try

               {

                   client.Send(message);

                   return "ok";

 

               }

               catch

               {

                   return "err1230";

 

               }

 

前端 AJAX 请求:

$.ajax({

        url: "Home/SendEmail",

        type:"post",

        data:"",

       beforeSend: function () { },

       success: function (data) {

           console.log(data);

           alert(data + "邮件发送成功!");

            //   alert(cpuMsg);

        },

       complete: function () { },

       error: function (ex) {

           alert("错误", "出错啦!");

           console.log(ex);

        }

    });

 

        }

但是在真正的邮箱里却没有发送接收的记录!懵逼。(邮件发送延迟很久!还以为又失败了!)


在发送方是没有发送记录的!在接收方有记录!

总结:邮箱发送其实很多细节,问题搞不清楚.QQ邮箱的 SMTP协议分明开启了,但是就是发不出去,搞半死,重新申请163邮箱,并且开启 SMTP协议任然不行。不知道要细节到哪里去!但是代码的实现方法时没有错的!

你可能感兴趣的:(.net)