2012-12-16 24 views
0

发送邮件,我有发送电子邮件我输入有效电子邮件,但艾当点击提交按钮,我得到这个错误,错误的问题:发送邮件失败错误:无法在asp.net

这里是我的代码(网页。配置)

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
     <!-- Default Port is [25] --> 
     <!-- Specific Port for Gmail is [587] NOT!!! [995]--> 
     <network defaultCredentials="false" host="mail.Sohasys.com" enableSsl="true" userName="[email protected]" 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("[email protected]", "", 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("[email protected]", 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); 
     } 

    } 

} 
+1

执行'Response.write(ex)'并发布_complete_错误消息。 –

+2

我有Response.Write(“Error:”+ ex.Message);在我的代码,这是完全错误心中已经得到了 –

+3

你只写邮件到响应 - 我建议 '的Response.Write(“错误:” + ex.ToString());' 得到stacktrace等等,因此更多关于错误的信息 –

回答

0

其可能你有错误的设置。

请尝试enableSsl =“假”

0

尝试请而不是主机“mail.Sohasys.com”把SMTP的IP地址在web.config中 。

,为什么你提到的Gmail特定端口...如果你用你为什么使用 “mail.Sohasys.com” 使用Gmail的SMTP谷歌,而不是

1

请在web.config中插入此

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

将enableSsl设置为false。