获取“发送电子邮件时发送电子邮件失败”

时间:2019-03-30 03:39:57

标签: c# .net asp.net-mvc

我正在尝试发送电子邮件,但出现以下错误

  

SocketException:连接尝试失败,因为一段时间后连接方未正确响应,或者建立的连接失败,因为连接的主机未能响应119.47.119.3:25

我要去哪里错了?

Index.html

@model Practice_Test.Models.ClsHomeContent

<div class="form-row">
    <div class="panel panel-default">

        <div class="panel-body">
            @using (Html.BeginForm("Index", "Home", FormMethod.Post))
            {
            <div class="form-group col-md-12">

                <label for="FullName">Full name</label><br />
                @Html.TextBoxFor(m => m.FullName)


                <label for="Email">Email</label><br />
                @Html.TextBoxFor(m => m.txtYourEmail)
             <br />

                <label for="lblYourMobile">Mobile</label><br />
                @Html.TextBoxFor(m => m.Mobile)<br />


                <label for="lblMessage">Message</label><br />
                @Html.TextAreaFor(m => m.txtMessage, new { @maxlength = "1000", style = "height:180px; width:900px;" })
                <br />

            </div>

                <div class="clearfix col-md-12 pull-left">
                    <button type="submit" id="submit" class="btn btn-primary pull-left">Send</button>
                </div>

            }
        </div>   

    </div>
</div>

index.cs

[HttpPost]
   public ActionResult Index(clsSMTPMail mail, ClsHomeContent model)
    {

            var toemail = new[] { "raj.yadav82@gmail.com" };

            MvcHtmlString msg = new MvcHtmlString(model.txtYourEmail + "<br/><br/>");
            var message = "Message from Guest  - " + msg + model.txtMessage;
            mail.SendNoAtt("noreply@Arion.co.nz", "", toemail,"", message, true, clsSMTPMail.ePriority.Medium);
            ModelState.Clear();

        return View(model);
    }

clsSMTPMail.cs

using System;
using System.Net.Mail;


    namespace Practice_Test
    {
        public class clsSMTPMail
        {
            public clsSMTPMail()
            {
                m_SMTPServer = "smtp.webhost.co.nz";
                m_portnumber = 25;
            }
            public clsSMTPMail(string SMTPServer)
            {
                m_SMTPServer = SMTPServer;
                m_portnumber = 25;
            }
            public clsSMTPMail(string SMTPServer, short PortNumber)
            {
                m_SMTPServer = SMTPServer;
                m_portnumber = PortNumber;
            }
            String m_SMTPServer;
            short m_portnumber;

            public enum ePriority
            {
                Low = 0,
                Medium = 1,
                High = 2
            }

            public void SendNoAtt(string fromAddress, string displayName, string[] toAddress, string subject, string body, bool sendAsHtml, ePriority priority)
            {
                string[] BlankAttachment = { };
                Send(fromAddress, displayName, toAddress, subject, body, sendAsHtml, priority, BlankAttachment);
            }

            public void Send(string fromAddress, string displayName, string[] toAddress, string subject, string body, bool sendAsHtml, ePriority priority, string[] attachments)
            {
                SmtpClient SMTPMail = new SmtpClient();

                {
                    MailMessage MailMsg = new MailMessage();
                    foreach (string Recipient in toAddress)
                    {
                        if ((Recipient != null))
                        {
                            MailMsg.To.Add(Recipient);
                            MailMsg.CC.Add(fromAddress);
                        }
                    }
                    MailMsg.From = new MailAddress(fromAddress.Trim(), displayName);
                    MailMsg.Subject = subject.Trim();
                    MailMsg.Body = body.Trim();
                    MailMsg.Priority = (MailPriority)priority;
                    MailMsg.IsBodyHtml = sendAsHtml;

                    if ((attachments != null))
                    {
                        foreach (string Attachment in attachments)
                        {
                            if (Attachment != "")
                            {
                                Attachment oMsgAttach = new Attachment(Attachment);
                                MailMsg.Attachments.Add(oMsgAttach);
                            }
                        }
                    }

                    SMTPMail.Host = m_SMTPServer;
                    SMTPMail.Port = m_portnumber;
                    SMTPMail.Send(MailMsg);
                    MailMsg.Dispose();
                    MailMsg = null;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试更改端口。

使用587 。一切正常。