Sending email from webform using SMTP server

≡放荡痞女 提交于 2021-01-29 06:01:44

问题


I have a ASP.NET web form for sending emails using SMTP. The hosting provider is 1and1. It worked for year. It stopped working several weeks ago. I receive the following error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"

1and1 support is unable to explain the reason for the change. I would appreciate your help (see the VB.NET code below).

I tried to change the port to 465 and 587, as well, I tried to change "EnableSsl" flag to true state - nothing worked. I wrote another code that works, but it allows to send an email with my email address in the "from" field (see the code below). But I would like to send an email with visitors' address in the "from" field.

    ' code that worked once
   Dim mailMessage As New MailMessage()
   Dim MailAddress As String                        
   Try
       'creating an instance of the MailMessage class
       mailMessage.From = New MailAddress(txtEmail.Text)
       'senders email address
       mailMessage.To.Add("me@mysite.net")
       'recipient's email address
       mailMessage.Subject = "Form Submission: " & txtSubject.Text
       'subject of the email message
      mailMessage.IsBodyHtml = False
      'message text format. Can be text or html
      mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
      'message body
      mailMessage.Priority = MailPriority.Normal
     'email priority. Can be low, normal or high
      Dim smtp As New SmtpClient("smtp.ionos.com")
     smtp.Port = 25
     smtp.EnableSsl = False
     smtp.Send(mailMessage)
     'using the static method "Send" of the SmtpMail class to send the mail
     Response.Write("Mail sent")
Catch ex As Exception
    lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me@mysite.net" 
End Try

' code that is working today, but my email address must be in the "from" field

Dim mailMessage As New MailMessage()
Dim MailAddress As String           

Try      
    'creating an instance of the MailMessage class
    mailMessage = New MailMessage()
    mailMessage.From = New MailAddress("me@mysite.net")
    'senders email address
    mailMessage.To.Add("me@mysite.net")
    'recipient's email address
    mailMessage.Subject = "Form Submission: " & txtSubject.Text
    'subject of the email message
    mailMessage.IsBodyHtml = False
    'message text format. Can be text or html
    mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
    'message body
    mailMessage.Priority = MailPriority.Normal
    'email priority. Can be low, normal or high
    Dim smtp As New SmtpClient'("smtp.ionos.com")
    smtp.UseDefaultCredentials = False
    smtp.Credentials = New Net.NetworkCredential("me@mysite.net", "MyPassword")
    smtp.Host = "smtp.ionos.com"
    smtp.Port = 587
    smtp.EnableSsl = True
    smtp.Send(mailMessage)
    'using the static method "Send" of the SmtpMail class to send the mail
    Response.Write("Mail sent")
Catch ex As Exception
    lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me@mysite.net"
End Try

来源:https://stackoverflow.com/questions/57762469/sending-email-from-webform-using-smtp-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!