好的,我输入后立即注意到可能涵盖同一问题的变体主题。我访问了他们中的大部分,发现与我提出的问题没有直接关系,所以我要求耐心。
无论如何,我正在使用VS2010创建一个ASP.NET Web应用程序。我正在尝试使用代码通过smtp发送电子邮件:
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(fromEmail);
mailMsg.To.Add(toEmail);
mailMsg.Subject = emailSubj.ToString().Trim();
mailMsg.Body = msgBody.ToString().Trim();
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMsg);
但每次我收到以下异常(SMTPException和innerException表示{"Unable to connect to the remote server"}
我还在web.config中定义了以下内容:
<system.net>
<mailSettings>
<smtp>
<network host="company.com"/>
</smtp>
</mailSettings>
</system.net>
我要做的是在提交带有请求ID的表单后发送电子邮件,以便可以通过其他页面访问(除了邮件之外的所有工作)。在我们使用Exchange服务器的公司中,当我转到我的联系人属性时,我得到smtp:emailaddress@company.com
那么这里要做什么?我已经检查了Web Services ExchangeServiceBinding
但是无法直接找到帮助我的东西(所以任何链接都赞赏)
非常感谢,期待阅读您的回复:)
答案 0 :(得分:2)
试试这个独立的C#应用程序,看看主机名是否有效。否则,您需要联系管理员以获取正确的地址。
/// <summary>
///Method to Send an Email informing interested parties about the status of data extraction.
/// INPUTS : int iProcessIsSuccess : int informing about the success of the process. -1 means failure, 0 means partial success, 1 means success.
/// string szLogDataToBeSent : Log data to be sent incase process not successful.
/// OUTPUTS : bool. True if success, else false.
/// </summary>
public bool SendEmailNotification(string szEmailAddressFileName, int iProcessIsSuccess, string szLogDataToBeSent)
{
bool bSuccess = false;
//the the SMTP host.
SmtpClient client = new SmtpClient();
//SMTP Server
client.Host = CommonVariables.EMAIL_SMTP_SERVER;
//SMTP Credentials
client.Credentials = new NetworkCredential(CommonVariables.EMAIL_USERNAME, CommonVariables.EMAIL_PASSWORD);
//Creating a new mail.
MailMessage mail = new MailMessage();
//Filling 'From' Tab.
mail.From = new MailAddress(CommonVariables.EMAIL_SENDERS_ADDRESS, CommonVariables.EMAIL_SENDERS_NAME);
//Filling 'To' tab.
List<EmailAddress> EmailAddressList = new List<EmailAddress>();
try
{
using (System.IO.FileStream fs = new FileStream(szEmailAddressFileName, FileMode.Open))
{
XmlSerializer xs = new XmlSerializer(typeof(List<EmailAddress>));
EmailAddressList = xs.Deserialize(fs) as List<EmailAddress>;
}
foreach (EmailAddress addr in EmailAddressList)
{
mail.To.Add(addr.RecepientEmailAddress);
}
}
catch(Exception Ex)
{
mail.To.Add("DefautEmailId@company.com");
}
//Filling mail body.
string szMailBody = "";
string szMailSubject = "";
if (1 == iProcessIsSuccess) //Success
{
szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a SUCCESS");
szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName);
szMailBody += "\r\n" + szMailSubject + "\r\n";
}
else if (0 == iProcessIsSuccess) //Partially Success
{
szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a PARTIAL SUCCESS"); ;
szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName);
szMailBody += "\r\n"+ szMailSubject + "\r\n";
szMailBody += "\r\n" + "The Log data is as follows:\r\n";
szMailBody += szLogDataToBeSent;
mail.Priority = MailPriority.High;
}
else //Failed
{
szMailSubject = String.Format(CommonVariables.EMAIL_SUBJECT_BOILER_PLATE, "a FAILURE"); ;
szMailBody = String.Format(CommonVariables.EMAIL_BODY_BOILER_PLATE, DateTime.UtcNow.ToString(), Environment.MachineName);
szMailBody += "\r\n" + szMailSubject + "\r\n";
szMailBody += "\r\n" + "The Log data is as follows:\r\n";
szMailBody += szLogDataToBeSent;
mail.Priority = MailPriority.High;
}
mail.Body = szMailBody;
mail.Subject = szMailSubject;
//Send Email.
try
{
client.Send(mail);
bSuccess = true;
}
catch (Exception Ex)
{
bSuccess = false;
}
// Clean up.
mail.Dispose();
return bSuccess;
}
}
答案 1 :(得分:1)
与您的系统管理员联系,获取您需要配置的Exchange Server的名称。
答案 2 :(得分:1)
有时smtp服务器与您正在查看的不同。 例如: 我的电子邮件是myemployee@mycompany.com, 我的实际smtp服务器是 server1.mail.mycompany.com,server2.mail.mycompany.com 您必须向管理员询问此服务器名称
之后询问您的用户是否在AD上定义,是否需要对每个smtp发送进行身份验证?
您的交换主机是否使用SMTP over TLS? AFAIK一些交换管理员使用SMTP over SSL或TLS声明。您可以通过获取其电子邮件的当前Exchange / Windows证书,查看有关使用SMTP over SSL或TLS发送的MSDN文档。