邮件没有通过 [英] Mail not going through

查看:100
本文介绍了邮件没有通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello fellas,



我一直在教自己MVC和以下教程我几乎已经完成了这个,除了我收到这个错误我无法弄清楚。



Hello fellas,

I have been teaching myself MVC and following tutorials and I have this almost done except I am getting this error I can''t figure out.

[HttpPost]
        public ActionResult Contact(ContactsModel model)
        {
            if (ModelState.IsValid)
            {
                InsertContact(model.Name, model.Phone, model.Email, model.Comments);

                MailMessage message = new MailMessage();
                message.From = new MailAddress(model.Email);
                message.To.Add(new MailAddress("lotusms@outlook.com"));
                message.Subject = "You've got mail!";
                message.Body = model.Comments;
                string[] args = { "Name : ", model.Name, "\nPhone : ", model.Phone, "\nComments : ", model.Comments };
                string strBody = String.Concat(args);
                SmtpClient client = new SmtpClient();
                client.Send(message);

                TempData["notice"] = "Someone will reply as soon as possible.";
                return RedirectToAction("Index", "Home");
            }

            return View();
            
        }





和我得到的错误是



指定的字符串不是电子邮件地址所需的格式。





and the error I m getting is

The specified string is not in the form required for an e-mail address.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.<br />
<br />
Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.







希望这会有所帮助。



万分感谢




Hope this helps.

Thanks a million

推荐答案

参考 - MailAddress构造函数(字符串) [ ^ ]。



此构造函数需要参数为一个 string ,它应该是电子邮件格式。



所以,放一个调试器并检查 model.Email的值,因为错误的电子邮件格式不正确。



[更新]
Web配置文件包含 system.net 节点,如下所示。

Refer - MailAddress Constructor (String)[^].

This constructor expects parameter to be one string and it should be in the email format.

So, put a debugger and check the value of model.Email, because it is not in the correct email format as per the error.

[Update]
The web config file includes the system.net node like below.
<mailsettings>
    <smtp from="http://djfitz.azurewebsites.net/">
      <network host="smtp.sendgrid.net" password="Password" username="username" port="25">
    </network></smtp>
  </mailsettings>





在这个地址被称为 http://djfitz.azurewebsites.net/ ,这不是一个

的电子邮件地址。



因此,当您调用默认构造函数时,它会从此处获取from地址并引发异常。当您从代码中传递地址时,您可以从这里删除,在这里提供正确的电子邮件地址。

[/ Update]


你已经问过这个问题这里 [ ^ ]。



显然你忽略了答案:你没有告诉SmtpClient用来发送消息的服务器,端口,用户名和密码。
You already asked this question here[^].

Apparently you''re ignoring the answer: You haven''t told the SmtpClient what server, port, username and password to use to send the message.


你缺少客户端。 EnableSsl = true;

解决方案:

You are missing client.EnableSsl=true;
Solution:
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);


这篇关于邮件没有通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆