2013-08-09 48 views
5

Desidero inviare un'email, ma mi dà un errore.Come si invia una e-mail gmail in vb.net?

ho questo codice:

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 

Ho un tentativo di cattura che cerca di chiamare questo metodo, si pretende molto lavorare così le piste di cattura e ottengo thes eccezione: 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 Perché ottengo questo errore? e come lo aggiusto?

risposta

9

Gmail utilizza SMTP su SSL sulla porta 465.

provare a fare:


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

Sì, questo è quello che pensavo fosse il problema con la porta, grazie – Arbitur

+0

Ok la porta che ho usato era quella giusta ma era l'EnableSsl di cui avevo bisogno. – Arbitur

4

Prova questa - so che funziona.

+0

Questa è solo una versione più lunga del mio codice ... – Arbitur

+1

La cosa da notare era la porta SMTP. Pensavo fosse 587, non 465 o 25. – iasanator

+0

Quindi dovrebbe essere 587 o 465? –

0

C'è qualche problema con l'account google, è necessario disattivare alcune impostazioni di sicurezza. Dopo l'invio di e-mail più e più volte, ho ricevuto e-mail su uno dei miei account di supporto (per Google), l'e-mail sono stati:

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 

Così ho acceso di sicurezza aggiuntiva e ho lavorato bene.

0

Cambiare la porta in 587. La porta 25 non supporta SSL.

0

Un modo super facile di fare questo (senza modificare le impostazioni di sicurezza) è quello di utilizzare IFTTT e la mia IFTTT Maker.net libary

In primo luogo, in IFTTT creare una nuova ricetta che è innescato dal canale Maker e il nome della manifestazione "send_gmail" .

Quindi, selezionare il motore di Gmail e fai clic su "Invia una e-mail", e sostituire a con {{valore1}}, soggetto con {{valore2}} e il messaggio/corpo con {{valore3}}

Dopo che, in Visual Studio, aggiungi ifttt.vb al tuo progetto. Ora per il codice:

 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 

Quindi inserire l'ID account. Puoi trovarlo su ifttt.com/maker

E questo è tutto!

0

Ho scritto la classe che può eseguire questa operazione facilmente.

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 

è il modo, come utilizzare classe:

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 

Spero che questo aiuti. Buona codifica