代码之家  ›  专栏  ›  技术社区  ›  kamiar3001

使用mailhelper.cs时出现电子邮件问题

  •  0
  • kamiar3001  · 技术社区  · 15 年前

    我有一个web应用程序,我将它设置为使用mailhelper.cs和web.config设置,通过gmail帐户自动发送电子邮件。我的应用程序已经运行了10天,现在它给了我发送电子邮件的错误和停止。我处理这个错误,它是“发送邮件失败”。内部异常是“试图对无法访问的主机执行套接字操作216.239.59.109:587”,下面是代码:

    <mailSettings>
      <smtp from="myemail@gmail.com">
        <network host="smtp.gmail.com"
                 port="587"
                 userName="myemail@gmail.com"
                 password="mypass"/>
      </smtp>
    </mailSettings>
    
    using System.Net.Mail;
    
    public class MailHelper
    {
       /// <summary>
       /// Sends an mail message
       /// </summary>
       /// <param name="from">Sender address</param>
       /// <param name="to">Recepient address</param>
       /// <param name="bcc">Bcc recepient</param>
       /// <param name="cc">Cc recepient</param>
       /// <param name="subject">Subject of mail message</param>
       /// <param name="body">Body of mail message</param>
       public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
       {
          // Instantiate a new instance of MailMessage
          MailMessage mMailMessage = new MailMessage();
    
          // Set the sender address of the mail message
          mMailMessage.From = new MailAddress(from);
          // Set the recepient address of the mail message
          mMailMessage.To.Add(new MailAddress(to)); 
          // Check if the bcc value is null or an empty string
          if ((bcc != null) && (bcc != string.Empty))
          {
             // Set the Bcc address of the mail message
             mMailMessage.Bcc.Add(new MailAddress(bcc));
          }
    
          // Check if the cc value is null or an empty value
          if ((cc != null) && (cc != string.Empty))
          {
             // Set the CC address of the mail message
             mMailMessage.CC.Add(new MailAddress(cc));
          }       // Set the subject of the mail message
          mMailMessage.Subject = subject;
          // Set the body of the mail message
          mMailMessage.Body = body; 
          // Set the format of the mail message body as HTML
          mMailMessage.IsBodyHtml = true;
          // Set the priority of the mail message to normal
          mMailMessage.Priority = MailPriority.Normal;
    
          // Instantiate a new instance of SmtpClient
                SmtpClient mSmtpClient = new SmtpClient();
                mSmtpClient.Host = "smtp.gmail.com";
                mSmtpClient.Port = 587;
                mSmtpClient.EnableSsl = true;
    
          // Send the mail message
          mSmtpClient.Send(mMailMessage);
       }
    }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Clarence Klopfstein    15 年前

    如Casperone的编辑所述,这似乎不是图书馆的问题,而是主机/Gmail服务器的问题。

    等式的一个方面是阻止通信。我倾向于把手指指向主机而不是Gmail。

    在这件事发生之前,你能发送多少电子邮件? 你的主人是谁?

    许多主机对它们允许您发送的传出电子邮件的数量有限制,因为它们不希望您使它们看起来像垃圾邮件发送者。

        2
  •  0
  •   casperOne    15 年前

    我强烈推荐安装 Elmah (或打开配置文件中的完整异常信息),以便在ASP.NET应用程序中捕获异常。一旦您这样做了,您应该能够提供更多关于异常的信息,然后您可以在这里复制这些信息。

    例外(我猜更重要的是这里的innerException属性)可能会告诉您您(或我们)需要的一切。


    基于此评论,您似乎在主机上遇到了允许您访问该地址的问题(或者端点拒绝您)。这似乎更像是配置/主机问题,而不是库问题。

    你能从你机器上的一个控制台应用程序上运行它吗?