2013-08-09 2 views
5

Ich möchte eine E-Mail senden, aber es gibt mir einen Fehler.Wie sende ich eine Gmail-E-Mail in vb.net?

Ich habe diesen Code:

Sub sendMail(ByVal title As String, ByVal content As String) 
    Dim SmtpServer As New SmtpClient("smtp.gmail.com", 25) 
    SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "password") 
    Dim mail As New MailMessage("[email protected]", "[email protected]", title, content) 
    SmtpServer.Send(mail) 
End Sub 

ich einen Versuch fangen haben, die zum Aufrufen dieser Methode versucht, tut es funktionieren so die Fang läuft und ich bekomme thes Ausnahme: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. b6sm3176487lae.0 - gsmtp Warum ich diese Fehlermeldung erhalten Sie? und wie repariere ich es?

Antwort

9

Gmail verwendet SMTP über SSL auf Port 465.

Versuchen Sie,:


    Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465) 
    ... 
    SmtpServer.EnableSsl = True 
    ... 
+0

Ja, das ist, was ich dachte, war das Problem mit dem Port, danke – Arbitur

+0

Ok der Port, den ich verwendete, war der richtige, aber es war die EnableSsl, die ich brauchte. – Arbitur

4

die Sie interessieren - ich weiß, es funktioniert.

Dim Mail As New MailMessage 
    Dim SMTP As New SmtpClient("smtp.gmail.com") 

    Mail.Subject = "Security Update" 
    Mail.From = New MailAddress("[email protected]") 
    SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "password") '<-- Password Here 

    Mail.To.Add(address & "@gmail.com") 'I used ByVal here for address 

    Mail.Body = "" 'Message Here 

    SMTP.EnableSsl = True 
    SMTP.Port = "587" 
    SMTP.Send(Mail) 
+0

Dies ist nur eine längere Version meines Codes ... – Arbitur

+1

Die Sache zu beachten war der SMTP-Port. Ich dachte, es war 587, nicht 465 oder 25. – iasanator

+0

Also soll es 587 oder 465 sein? –

0

Es gibt ein Problem mit Google-Konto, Sie müssen einige Sicherheitseinstellungen deaktivieren. E-Mail über und über Nach der Sendung, ich E-Mail auf einem meines Support-Konto erhalten hat (Google), war die E-Mail:

You recently changed your security settings so that your Google Account [[email protected]] is no longer protected by modern security standards. 

If you did not make this change 
Please review your Account Activity page at https://security.google.com/settings/security/activity to see if anything looks suspicious. Whoever made the change knows your password; we recommend that you change it right away. 

If you made this change 
Please be aware that it is now easier for an attacker to break into your account. You can make your account safer again by undoing this change at https://www.google.com/settings/security/lesssecureapps then switching to apps made by Google such as Gmail to access your account. 
Sincerely, 
The Google Accounts team 

Also wechselte ich von zusätzlicher Sicherheit und i hatte gut funktioniert.

0

Ändern Sie den Port auf 587. Port 25 unterstützt kein SSL.

0

Ein super einfache Möglichkeit, dies zu tun (ohne Sicherheitseinstellungen zu ändern) ist durch IFTTT und meine IFTTT Maker.net libary

Erste Verwendung in IFTTT ein neues Rezept erstellen, die von der Maschine Kanal ausgelöst wird und den Namen der Veranstaltung „send_gmail“ .

Dann wählen Sie den Google Mail-Engine und klicken Sie auf "Senden Sie eine E-Mail", und ersetzen, um mit {{value1}}, Thema mit {{value2}} und Nachricht/Körper mit {{value3}}

Nach Fügen Sie in Visual Studio ifttt.vb Ihrem Projekt hinzu. Jetzt für den Code:

 Try 
    makechannel.scode = "your account ID" 
    makechannel.fireevent("send_gmail", "TO", "SUBJECT", "MESSAGE") 
    'code goes here if done 
    Catch ex As Exception 
     'code goes here if it fails 
    End Try 

Geben Sie dann Ihre Konto-ID ein. Sie finden es unter ifttt.com/maker

Und das ist es!

0

Ich habe die Klasse geschrieben, die diese Aufgabe leicht ausführen kann.

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 

Es ist die Art und Weise, wie Klasse verwenden:

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 

Hoffnung, das hilft. Gute Codierung