2011-11-07 149 views
0

我在下面的代碼中缺少什麼?發送電子郵件失敗。vb.net發送郵件失敗

Private Sub sendTestEmail() 

Dim EmailBody As String 

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


Dim TestEmail As New System.Net.Mail.MailMessage("[email protected]", "[email protected]", "TestEmail", EmailBody) 

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

End Sub 
+3

完全是什麼信息?錯誤細節很重要。 – David

+2

我敢打賭,你的盒子沒有正確設置爲一個smptp服務器。如果您使用Widnows,請轉到InetPub \ mailroot;我相信你會在那裏看到你所有的電子郵件,等待發送 – Icarus

+0

你有本地主機上運行的SMTP服務器嗎? –

回答

2

看起來你的代碼here

來到這可能是您的SMTP服務器未安裝或本地主機配置。

這也可能是一個安全問題,阻止您的本地SMTP服務器轉發郵件。

0

我寫了可以輕鬆完成這個任務的類。

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 

它的方法,如何使用類:

Dim GGmail As New GGSMTP_GMAIL("[email protected]", "AccPassword",) 

     Dim ToAddressies As String() = {"[email protected]", "[email protected]"} 
     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 

希望這有助於。良好的編碼