简体   繁体   English

vb.net发送邮件失败

[英]vb.net send mail is failing

What am I missing in the code below? 我在下面的代码中缺少什么? I am getting a failure sending email message. 我收到发送电子邮件的失败。

Private Sub sendTestEmail()

 Dim EmailBody As String

 EmailBody = "This is a test *****************"


 Dim TestEmail As New System.Net.Mail.MailMessage("webserver@companyname.com",   "Smilinglily@companyname.com", "TestEmail", EmailBody)

 Dim EmailServer As New System.Net.Mail.SmtpClient("localhost")
 EmailServer.SendAsync(TestEmail, Me)

End Sub

Looks like your code came from here 看起来你的代码来自这里

It is probably that your SMTP server is not installed or configured on localhost. 可能是您的SMTP服务器未在localhost上安装或配置。

It could also be a security problem stopping your localhost SMTP server from forwarding the mail. 阻止localhost SMTP服务器转发邮件也可能是一个安全问题。

I Have written the class which can perform this task easyly. 我已经编写了可以轻松执行此任务的类。

Imports System.Net.Mail
Public Class GGSMTP_GMAIL
    Dim Temp_GmailAccount As String
    Dim Temp_GmailPassword As String
    Dim Temp_SMTPSERVER As String
    Dim Temp_ServerPort As Int32
    Dim Temp_ErrorText As String = ""
    Dim Temp_EnableSSl As Boolean = True
    Public ReadOnly Property ErrorText() As String
        Get
            Return Temp_ErrorText
        End Get
    End Property
    Public Property EnableSSL() As Boolean
        Get
            Return Temp_EnableSSl
        End Get
        Set(ByVal value As Boolean)
            Temp_EnableSSl = value
        End Set
    End Property
    Public Property GmailAccount() As String
        Get
            Return Temp_GmailAccount
        End Get
        Set(ByVal value As String)
            Temp_GmailAccount = value
        End Set
    End Property
    Public Property GmailPassword() As String
        Get
            Return Temp_GmailPassword
        End Get
        Set(ByVal value As String)
            Temp_GmailPassword = value
        End Set
    End Property
    Public Property SMTPSERVER() As String
        Get
            Return Temp_SMTPSERVER
        End Get
        Set(ByVal value As String)
            Temp_SMTPSERVER = value
        End Set
    End Property
    Public Property ServerPort() As Int32
        Get
            Return Temp_ServerPort
        End Get
        Set(ByVal value As Int32)
            Temp_ServerPort = value
        End Set
    End Property
    Public Sub New(ByVal GmailAccount As String, ByVal GmailPassword As String, Optional ByVal SMTPSERVER As String = "smtp.gmail.com", Optional ByVal ServerPort As Int32 = 587, Optional ByVal EnableSSl As Boolean = True)
        Temp_GmailAccount = GmailAccount
        Temp_GmailPassword = GmailPassword
        Temp_SMTPSERVER = SMTPSERVER
        Temp_ServerPort = ServerPort
        Temp_EnableSSl = EnableSSl
    End Sub
    Public Function SendMail(ByVal ToAddressies As String(), ByVal Subject As String, ByVal BodyText As String, Optional ByVal AttachedFiles As String() = Nothing) As Boolean
        Temp_ErrorText = ""
        Dim Mail As New MailMessage
        Dim SMTP As New SmtpClient(Temp_SMTPSERVER)
        Mail.Subject = Subject
        Mail.From = New MailAddress(Temp_GmailAccount)
        SMTP.Credentials = New System.Net.NetworkCredential(Temp_GmailAccount, Temp_GmailPassword) '<-- Password Here
        Mail.To.Clear()
        For i As Int16 = 0 To ToAddressies.Length - 1
            Mail.To.Add(ToAddressies(i))
        Next i
        Mail.Body = BodyText
        Mail.Attachments.Clear()

        If AttachedFiles IsNot Nothing Then
            For i As Int16 = 0 To AttachedFiles.Length - 1
                Mail.Attachments.Add(New Attachment(AttachedFiles(i)))
            Next
        End If

        SMTP.EnableSsl = Temp_EnableSSl
        SMTP.Port = Temp_ServerPort

        Try
            SMTP.Send(Mail)
            Return True
        Catch ex As Exception
            Me.Temp_ErrorText = ex.Message.ToString
            Return False
        End Try

    End Function
End Class

Its the way, how to use class: 它的方式,如何使用类:

Dim GGmail As New GGSMTP_GMAIL("MyFromAddress1@gmail.com", "AccPassword", )

        Dim ToAddressies As String() = {"ToAddress1@gmail.com", "ToAddress2@gmail.com"}
        Dim attachs() As String = {"d:\temp_Excell226.xlsx", "d:\temp_Excell224.xlsx", "d:\temp_Excell225.xlsx"}
        Dim subject As String = "My TestSubject"
        Dim body As String = "My text goes here ...."
        Dim result As Boolean = GGmail.SendMail(ToAddressies, subject, body, attachs)
        If result Then
            MsgBox("mails sended successfully", MsgBoxStyle.Information)
        Else
            MsgBox(GGmail.ErrorText, MsgBoxStyle.Critical)
        End If 

Hope this helps. 希望这可以帮助。 Good coding 好编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM