Updating Legacy Code from System.Web.Mail to System.Net.Mail in Visual Studio 2005: Problems Sending Email

Using an outdated System.Web.Mail email message works fine, here is a 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 is 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 tried a lot of different options and nothing works, I have a feeling that this might be related to SmtpClient, is there something that has changed in the base code between these versions?

There are no exceptions thrown back.

+3
source share
6 answers

, . , , , (MAIL_SERVER) .

, (telnet, ) , .

EDIT: ( ), . , 25 (SMTP) 587 () - . , .

Wikipedia rfc4409 .

0

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>
+1

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)

-

0

E-Mail?

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

, , .

0

, , . .

  • SMTP IIS.
  • , .

, .

, MsgBox (ex.Message). , MessageBox asp.net , , . .

0
source

I added a port number for the mail server, and it started to work sporadically, it seems that this is a server problem and a delay in sending messages. Thanks for your answers, they were helpful!

0
source

Source: https://habr.com/ru/post/1696758/


All Articles