Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Legacy Code from System.Web.Mail to System.Net.Mail in Visual Studio 2005: Problems sending E-Mail

Using the obsolete System.Web.Mail sending email works fine, here's the code snippet:

 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

and here's the updated version:

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

I have tried many different variations and nothing seems to work, I have a feeling it may have to do with the SmtpClient, is there something that changed in the underlying code between these versions?

There are no exceptions that are thrown back.

like image 866
DoryuX Avatar asked Nov 14 '22 17:11

DoryuX


1 Answers

The System.Net.Mail library uses the config files to store the settings so you may just need to add a section like this

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
like image 158
pete blair Avatar answered Dec 14 '22 23:12

pete blair