发送邮件失败 - 无法连接到远程服务器 [英] Failure sending mail - unable to connect to remote server

查看:550
本文介绍了发送邮件失败 - 无法连接到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这看起来很简单,但我无法让它正常工作。

我正在尝试从我的应用程序发送电子邮件。一个简单的查询表格。

它需要用户的地址,姓名,联系方式等,并向客户的电子邮件ID发送电子邮件。

以下是代码。

Hi,
This looks pretty simple but I am not able to get it working.
I am trying to send an email from my application. A simple Enquiry form.
It takes user's address, name, contact no etc and sends an email to Customer's email id.
Below is the code.

SmtpClient client = new SmtpClient("ip address");
client.EnableSsl = true;
client.Timeout = 20000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("user@domain.com", "password"); //webmail.domain.com
MailMessage msg = new MailMessage();
msg.To.Add(txtEmailId.Text);
msg.From = new MailAddress("user@domain.com");
msg.Subject = "Test";
msg.Body = txtComments.Text;
client.Send(msg);



这会出错,操作超时。所以我将超时时间从20000增加到50000.
但没有运气。



客户使用网络邮件。

代码是由其他开发人员编写的,我不确定以前也在使用。

我可以ping通为SMTP服务器提供的IP地址。



任何帮助都会有很大的帮助。



谢谢,

Lok



我尝试了什么:



我试过改变超时持续时间。但是我又犯了一个错误。 发送邮件失败。无法连接到远程服务器。


This gives an error, "Operation timed out." So I increased the timeout from 20000 to 50000.
But no luck.

The customer uses webmail.
The code has been written by other developers which I am not sure was working previously also.
I am able to ping the ip address given for SMTP server.

Any help in this would be of great help.

Thanks,
Lok

What I have tried:

I tried changing the Timeout duration. But I got another error. "Failure sending mail. Unable to connect to remote server."

推荐答案

您好,



如何ping服务器,它正在运行。

尝试以下功能。没问题。

代码与您的代码非常相似,但我不使用您使用的某些属性。

我在我工作的所有项目中使用它。



Hi,

How you ping your server, it is working.
Try the function below. It's fine.
The code is very similar with your code, but I don't use some property that you use.
I use it on all projects in my work.

public static string SendEmail(string strFrom, string strTo, string strCc, string strBcc, string strSubject, string strBody, string strAttach)
    {
        
      
  
      string strResults = "";
      MailMessage objEmail = null;
      SmtpClient objSmtp;
      try
      {
          objEmail = new MailMessage();
          //from email
          objEmail.From = new MailAddress(strFrom);
          objEmail.To.Add(strTo.Replace(";", ","));// To add more than recipient, separete for comma. 

	  //ocult copy 	
          if (strBcc != "")
              objEmail.Bcc.Add(strBcc.Replace(";", ","));// To add more than recipient, separete for comma. 

          //prioridade do email
          objEmail.Priority = MailPriority.Normal;

          //use true to enable html email, or false, to only text 
          objEmail.IsBodyHtml = true;
          objEmail.Subject = strSubject;    
          objEmail.Body = strBody;
          
          objEmail.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
          objEmail.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");

          //verify if you have attachment file
          if (strAttach != "")
          {
              Attachment objitemfile = new Attachment(strAttach);
              objEmail.Attachments.Add(objitemfile);
          }


          //create smtp client
          objSmtp = new SmtpClient();
          //add email server address 
          objSmtp.Host = "myserveremail@domain.com";

          //e-mail server authentication
          //objSmtp.Credentials = new NetworkCredential("username", "password");

          //send e-mail
          objSmtp.Send(objEmail);
          strResults = "OK";
      
      }
      catch (System.Exception erro)
      {
          strResults = " 1ª - HelpLink ==> " + erro.HelpLink;
          strResults += "\n\n 2ª - InnerException = " + erro.InnerException;
          strResults += "\n\n 3ª - Message = " + erro.Message;
          strResults += "\n\n 4ª - Source = " + erro.Source;
          strResults += "\n\n 5ª - StackTrace = " + erro.StackTrace;
          strResults += "\n\n 6ª - TargetSite = " + erro.TargetSite;
      }
      finally
      {
          objSmtp = null;
          objEmail.Dispose();
      }

      return strResults;
    }


这篇关于发送邮件失败 - 无法连接到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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