本文介绍了无法连接到远程服务器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下,
Hi,
my code as below,
public void SendMaiL(string sSubject, string[] arToAddress, string[] arToAddressCC, int? iReqEmpId, int iMailType)
{
string sBody = string.Empty;
bool enableSSL = true;
if (arToAddress != null && arToAddress.Length > 0)
{
try
{
string sServer = "smtp.mail.com";
int iPort =587;
string sUserName = ConfigurationManager.AppSettings["userName"].ToString();
string sPassword = ConfigurationManager.AppSettings["password"].ToString();
string sFrom = ConfigurationManager.AppSettings["from"].ToString();
//oSmtpClient.UseDefaultCredentials = false;
//oSmtpClient.Credentials = new NetworkCredential(sUserName, sPassword);
MailMessage oMailMessage = new MailMessage(); ;
oMailMessage.From = new MailAddress(sFrom);
if (arToAddress != null && arToAddress.Length > 0)
{
sBody = GetMailBody(Convert.ToInt32(arToAddress[0]), iMailType, iReqEmpId);
for (int t = 0; t <= arToAddress.Length - 1; t++)
{
oMailMessage.To.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddress[t].ToString()))));
}
}
if (arToAddressCC != null && arToAddressCC.Length > 0)
{
for (int i = 0; i <= arToAddressCC.Length - 1; i++)
{
if (arToAddressCC[i] != null)
oMailMessage.CC.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddressCC[i].ToString()))));
}
}
oMailMessage.Subject = sSubject;
oMailMessage.Body = sBody;
oMailMessage.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(sServer, iPort))
{
smtp.Credentials = new NetworkCredential(sFrom, sPassword);
smtp.EnableSsl = enableSSL;
smtp.Send(oMailMessage);
}
}
catch (Exception ex)
{
//WriteLog(ex.ToString());
}
}
}
但是获取错误smtp异常被捕获。发送邮件失败。无法连接到远程服务器。
:(
谢谢..
But getting error smtp exception was caught.Failure sending mail. Unable to connect to the remote server.
:(
thanks..
推荐答案
string sServer = "smtp.mail.com";
你真的打算输入:
Did you actually mean to type:
string sServer = "smtp.gmail.com";
这篇关于无法连接到远程服务器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!