发送电子邮件在本地工作,但不能从远程服

时间:2009-09-18 11:29:46

标签: c#

我正在通过以下功能发送电子邮件,当我从本地服务器运行它时工作正常,但是当我远程运行它时它会失败。可能导致这个问题的原因是什么?

private void SendEmail()
{
    try
    {
        MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient("smtp.gmail.com",587);
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        NetworkCredential loginInfo = new NetworkCredential("xx@gmail.com", "xxxx");
        message.From = new MailAddress("xx@gmail.com", "xx");
        message.To.Add(new MailAddress("yyy@zzz.ac.in","Mail"));
        message.IsBodyHtml = true ;
        string emailContent = "ICHE 2010 - Abstract Received <br><br>Title: " + Abstract_Title.Text + "<br><br>Author: " + TxtAuthor_FirstName.Text + "_" + TxtAuthor_LastName.Text + "<br><br>Abstract in pdf format attached with this email. <br><br> ICHE2010 Website";
        message.Body = emailContent;
        message.Subject = "ICHE 2010 - Abstract Received";
        string FileName = Server.MapPath(Request.ApplicationPath + "\\AbstractPdfs" + "\\" + abstractBO.AbstractFileNameWithTicks);
        Attachment attachmentpdf = new System.Net.Mail.Attachment(FileName);
        message.Attachments.Add(attachmentpdf);
        client.EnableSsl = true;
        client.Send(message);
    }
    catch (SmtpException smtpex)
    {
        throw smtpex;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

2 个答案:

答案 0 :(得分:1)

可能是防火墙阻止您的应用程序使用该端口发送电子邮件。或者您的远程服务器可能没有Internet连接。这可能有很多原因。请解释一下。

答案 1 :(得分:0)

你需要检查两件事:

  • 从您的代码中,检查端口587是否未被阻止或已启用
  • 同时尝试打开端口25,这是SMTP传统上使用的端口