views:

731

answers:

6

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.

A: 

Have you tried adding

smtp.UseDefaultCredentials = True

before the send?

Also, what happens if you try changing:

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

to this:

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

-- Kevin Fairchild

Kevin Fairchild
A: 

I've tested your code and my mail is sent successfully. Assuming that you're using the same parameters for the old code, I would suggest that your mail server (MAIL_SERVER) is accepting the message and there's a delay in processing or it considers it spam and discards it.

I would suggest sending a message using a third way (telnet if you're feeling brave) and see if that is successful.

EDIT: I note (from your subsequent answer) that specifying the port has helped somewhat. You've not said if you're using port 25 (SMTP) or port 587 (Submission) or something else. If you're not doing it already, using the sumission port may also help solve your problem.

Wikipedia and rfc4409 have more details.

Robin M
A: 

Are you setting the credentials for the E-Mail?

smtp.Credentials = New Net.NetworkCredential("[email protected]", "password")

I had this error, however I believe it threw an exception.

David Basarab
+1  A: 

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>
pete blair
he's passing the smtp server into the constructor of SmtpClient.
Darren Kopp
A: 

Everything you are doing is correct. Here's the things i would check.

  1. Double check that the SMTP service in IIS is running right.
  2. Make sure it's not getting flagged as spam.

those are usually the biggest culprits whenever we have had issues w/ sending email.

Also, just noticed you are doing MsgBox(ex.Message). I believe they blocked MessageBox from working asp.net in a service pack, so it might be erroring out, you just might not know it. check your event log.

Darren Kopp
A: 

I added the port number for the mail server and it started working sporadically, it seems that it was a problem with the server and a delay in sending the messages. Thanks for your answers, they were all helpful!

DoryuX