【问题标题】:Updating Legacy Code from System.Web.Mail to System.Net.Mail in Visual Studio 2005: Problems sending E-Mail在 Visual Studio 2005 中将旧代码从 System.Web.Mail 更新到 System.Net.Mail:发送电子邮件时出现问题
【发布时间】:2008-09-09 16:41:00
【问题描述】:

使用过时的 System.Web.Mail 发送电子邮件可以正常工作,这里是代码 sn-p:

 Public Shared Sub send(ByVal recipent As String, ByVal from As String, ByVal subject As String, ByVal body As String)
        Try
            Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
            Message.To = recipent
            Message.From = from
            Message.Subject = subject
            Message.Body = body
            Message.BodyFormat = MailFormat.Html
            Try
                SmtpMail.SmtpServer = MAIL_SERVER
                SmtpMail.Send(Message)
            Catch ehttp As System.Web.HttpException
                critical_error("Email sending failed, reason: " + ehttp.ToString)
            End Try
        Catch e As System.Exception
            critical_error(e, "send() in Util_Email")
        End Try
    End Sub

这是更新的版本:

Dim mailMessage As New System.Net.Mail.MailMessage()

        mailMessage.From = New System.Net.Mail.MailAddress(from)
        mailMessage.To.Add(New System.Net.Mail.MailAddress(recipent))

        mailMessage.Subject = subject
        mailMessage.Body = body

        mailMessage.IsBodyHtml = True
        mailMessage.Priority = System.Net.Mail.MailPriority.Normal

        Try

            Dim smtp As New Net.Mail.SmtpClient(MAIL_SERVER)
            smtp.Send(mailMessage)

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try

我尝试了许多不同的变体,但似乎没有任何效果,我觉得这可能与 SmtpClient 有关,这些版本之间的底层代码是否发生了变化?

没有被抛出的异常。

【问题讨论】:

    标签: .net vb.net email visual-studio-2005 .net-2.0


    【解决方案1】:

    System.Net.Mail 库使用配置文件来存储设置,因此您可能只需要添加这样的部分

      <system.net>
        <mailSettings>
          <smtp from="test@foo.com">
            <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
          </smtp>
        </mailSettings>
      </system.net>
    

    【讨论】:

    • 他正在将 smtp 服务器传递给 SmtpClient 的构造函数。
    【解决方案2】:

    我已经测试了您的代码,并且我的邮件已成功发送。假设您对旧代码使用相同的参数,我建议您的邮件服务器 (MAIL_SERVER) 正在接受该邮件并且处理延迟或它认为它是垃圾邮件并丢弃它。

    我建议使用第三种方式发送消息(如果您觉得勇敢,请使用 telnet),看看是否成功。

    编辑:我注意到(从您随后的回答中)指定端口有所帮助。您还没有说您使用的是端口 25(SMTP)还是端口 587(提交)或其他。如果您还没有这样做,使用 sumission 端口也可能有助于解决您的问题。

    Wikipediarfc4409 有更多详细信息。

    【讨论】:

      【解决方案3】:

      你有没有尝试添加

      smtp.UseDefaultCredentials = True 
      

      发送前?

      另外,如果你尝试改变会发生什么:

      mailMessage.From = New System.Net.Mail.MailAddress(from)
      mailMessage.To.Add(New System.Net.Mail.MailAddress(recipent))
      

      到这里:

      mailMessage.From = New System.Net.Mail.MailAddress(from,recipent)
      

      -- 凯文·费尔柴尔德

      【讨论】:

        【解决方案4】:

        您是否正在设置电子邮件的凭据?

        smtp.Credentials = New Net.NetworkCredential("xyz@gmail.com", "password")
        

        我遇到了这个错误,但我相信它引发了异常。

        【讨论】:

          【解决方案5】:

          你所做的一切都是正确的。这是我要检查的内容。

          1. 仔细检查 IIS 中的 SMTP 服务是否正常运行。
          2. 确保它没有被标记为垃圾邮件。

          当我们在发送电子邮件时遇到问题时,这些通常是最大的罪魁祸首。

          另外,刚刚注意到您正在执行 MsgBox(ex.Message)。我相信他们阻止 MessageBox 在服务包中工作 asp.net,所以它可能会出错,你可能不知道。检查您的事件日志。

          【讨论】:

            【解决方案6】:

            我添加了邮件服务器的端口号,它开始偶尔工作,似乎是服务器的问题和发送消息的延迟。感谢您的回答,他们都很有帮助!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-04-02
              • 2012-08-20
              相关资源
              最近更新 更多