错误:在asp.net中发送邮件失败

时间:2012-12-16 11:40:29

标签: c# asp.net email web-config sendmail

我有发送电子邮件的问题我输入有效的电子邮件但是当我点击提交按钮时出现此错误,错误:发送邮件失败

这是我的代码(web.Config)

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Soha@sohasys.com">
        <!-- Default Port is [25] -->
        <!-- Specific Port for Gmail is [587] NOT!!! [995]-->
        <network defaultCredentials="false" host="mail.Sohasys.com" enableSsl="true" userName="Soha@sohasys.com" password="XXXX" port="587"/>
      </smtp>
    </mailSettings>
  </system.net>

这是我的代码

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        try
        {
            System.Net.Mail.MailMessage oMailMessage = new System.Net.Mail.MailMessage();

            oMailMessage.IsBodyHtml = true;
            oMailMessage.Priority = System.Net.Mail.MailPriority.Normal;
            oMailMessage.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.Never;

            System.Net.Mail.MailAddress oMailAddress = null;

            oMailAddress =
                new System.Net.Mail.MailAddress(txtEmail0.Text, ttxtPhone.Text, System.Text.Encoding.UTF8);

            oMailMessage.From = oMailAddress;
            oMailMessage.Sender = oMailAddress;
            oMailMessage.ReplyTo = oMailAddress;

            oMailAddress =
                new System.Net.Mail.MailAddress("Soha@sohasys.com", "", System.Text.Encoding.UTF8);
            oMailMessage.To.Add(oMailAddress);

            // oMailMessage.CC
            // oMailMessage.Bcc

            oMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
            oMailMessage.Subject = "(From WebSite)" + " : " + txtUsername.Text;

            oMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            oMailMessage.Body = txtBody.Text;

            System.Net.Mail.SmtpClient oSmtpClient = new System.Net.Mail.SmtpClient("Soha@sohasys.com", 587);


              oSmtpClient.EnableSsl = true;
            oSmtpClient.Timeout = 100000;

           // oSmtpClient.UseDefaultCredentials = false;

         //  ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

            oSmtpClient.Send(oMailMessage);
            Label5.Visible = true;
            Label5.Text = "Your Email Send SuccesFully.";

        }
        catch (System.Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }

    }

}

3 个答案:

答案 0 :(得分:1)

请在web.config

中插入
system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="Soha@sohasys.com">
    <!-- Default Port is [25] -->
    <!-- Specific Port for Gmail is [587] NOT!!! [995]-->
    <network defaultCredentials="false" host="mail.Sohasys.com" enableSsl="false" userName="Soha@sohasys.com" password="XXXX" port="587"/>
  </smtp>
</mailSettings>

将enableSsl设置为false。

答案 1 :(得分:0)

可能你的设置错误。

请尝试enableSsl =“false”

答案 2 :(得分:0)

请尝试请代替主机“mail.Sohasys.com”将smtp的ip地址放入 web.config中。

为什么你提到gmail特定端口...如果你使用gmail smtp为什么使用 “mail.Sohasys.com”改用谷歌

相关问题