Send email in VB.Net with tls enabled server

℡╲_俬逩灬. 提交于 2020-02-06 08:43:08

问题


I am developing application for sending an email with TLS enabled SMTP server and this application I want to run on windows server 2003. When I run this same application on window server 2012 R2 its working perfect but it wont work on window server 2003. Is there any specific reason it wont work on window server 2003?

Error: SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.

I used below code in my application:

Public Sub sendemail()
    Dim SMTPMailServer As New System.Net.Mail.SmtpClient("xyz") 'tls enabled SMTP Server Name 
    Dim myMail As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage("FromEmail", "ToEmail")
    With myMail
        .Subject = "Test Email with TLS enabled server"
        .Body = "Test Body"
        .Priority = Net.Mail.MailPriority.Normal
        .IsBodyHtml = True
    End With
    SMTPMailServer.Send(myMail)
    myMail = Nothing
End Sub

回答1:


Imports System.Net.Mail
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
        Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("YOURusername@gmail.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class


来源:https://stackoverflow.com/questions/59372731/send-email-in-vb-net-with-tls-enabled-server

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