System.Net.Mail没有发送我想要的信息 [英] System.Net.Mail not sending the information I want

查看:65
本文介绍了System.Net.Mail没有发送我想要的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿fellas!



我有一个联系表格的以下代码发送到我的电子邮件地址



Hey fellas!

I have the following code for a contact form that is sent to my email address

[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("sender1@foo.bar.com");
                message.To.Add(new MailAddress("lotusms@outlook.com"));
                message.Subject = "This is my subject";
                message.Body = "This is the content";
                SmtpClient client = new SmtpClient();
                client.Send(message);

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

            return View();
            
        }





好​​消息是它有效。坏消息是:



1.它进入垃圾文件夹。有没有办法解决这个问题?

2.它发送一封来自sender1@foo.bar.com的电子邮件给我发送这是我的内容而不是textarea中的实际内容。



我怎样才能使消息。从实际上发送在电子邮件文本框中输入的电子邮件地址,以及message.Body发送TextArea的内容。



这是我的表格。





The good news is that it works. The bad news are:

1. It goes to the junk folder. Is there a way to get around this?
2. It sends an email from sender1@foo.bar.com to me with a "This is my content" instead of the actual content in the textarea.

How can I make it so that the message.From actually sends the email address entered in the email textbox, and the message.Body to send the content of the TextArea.

Here is my form as well.

<div id="contact-form">
            @using(Html.BeginForm()) 
            {
                @Html.LabelFor(model => model.Name)
                @Html.TextBoxFor(model => model.Name)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Name)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Phone)
                @Html.TextBoxFor(model => model.Phone)<br class="clear" />    
                @Html.ValidationMessageFor(model => model.Phone)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Email)
                @Html.TextBoxFor(model => model.Email)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Email)<br class="clear" /> <br />
    
                @Html.LabelFor(model => model.Comments)
                @Html.TextAreaFor(model => model.Comments, 10, 40, null)<br class="clear" /> 
                @Html.ValidationMessageFor(model => model.Comments)<br class="clear" /> <br />
    
        
                <button type="submit">SEND</button> 
                <button type="reset">CLEAR</button> 
            }    
        </div>

谢谢

推荐答案

这样做是为了指定正确的目的地址:

Do this to specify the correct destination address:
message.To.Add(new MailAddress(model.Email));



/ ravi


/ravi


电子邮件将转到垃圾文件夹,因为您没有提供STMP在SMTP服务器上注册的主机名和正版电子邮件地址。需要主机名来验证来自哪里的电子邮件。
Email is going to junk folder as your not providing STMP host name and genuine email address registered with SMTP server. Host name is required to authenticate email from where they are coming.


您好,



您正在对这些值进行硬编码。尝试更改代码,如下所示。我想你一定错过了,因为在数据库中插入记录时你使用了正确的值。

Hello,

You are hard-coding the values. Try changing the code as shown below. I think you must have missed it, because while inserting a record in database you are using correct values.
[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 = "This is my subject";
        message.Body = model.Comments;
        SmtpClient client = new SmtpClient();
        client.Send(message);
 
        TempData["notice"] = "Someone will reply as soon as possible.";
        return RedirectToAction("Index", "Home");
    }
    return View();
}



问候,


Regards,


这篇关于System.Net.Mail没有发送我想要的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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