Send mail

Bonjour,
bonjour

Avant d'intégrer l'option d'envoi d'email via VB dans mon projet professionnel, j'essaie en vain de le faire par mes adresses personnels.

J'ai ajouté la référence system.net (?)

mis 2 boutons sur un form pour faire le choix entre hotmail et gmail, mais ca ne fonctionne pas ? (j'ai parcouru le forum mais après plusieurs "mise au point" ....si il ya une ou plusieurs âmes pour m'aider, merci, aurelie.

ci dessous le code :

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 NetworkCredential("prenom.nom@hotmail.fr", "password")
            SmtpServer.Port = 465 ' aussi essayé aussi : 25,587,2525
            SmtpServer.Host = "smtp.live.com"
            mail = New MailMessage()
            mail.From = New MailAddress("prenom.nom@hotmail.fr")
            mail.To.Add("prenom.nom@gmail.com")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox("mail no send")
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New NetworkCredential("prenom.nom@gmail.com", "password")
            SmtpServer.Port = 2525  ' aussi essayé aussi : 25,587,2525
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("prenom.nom@gmail.com")
            mail.To.Add("nom.prenom@hotmail.fr")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox("mail no send")
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class


2 réponses

  1. Bonjour,

    J'ai développé un code pour VBA qui fonctionne parfaitement avec un compte Gmail et donc avec le serveur smtp.gmail.com .

    Dans votre cas pour VB, 2 éléments doivent attirer votre attention:
    le nom du serveur "smtp.live.com" au lieu de "smtp.gmail.com"
    et surtout pas d'activation du mode SSL.
    Il doit être possible aussi en VB d'utiliser le module CDO.


    Sub EnvoiMail()
    'Add the Project Reference Microsoft CDO WINDOWS FOR 2000
    Dim cdo_msg As New CDO.Message

    'configuration message
    cdo_msg.Configuration.Fields(cdoSMTPServer) = "smtp.gmail.com"
    cdo_msg.Configuration.Fields(cdoSMTPConnectionTimeout) = 60
    cdo_msg.Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
    cdo_msg.Configuration.Fields(cdoSMTPServerPort) = 465
    cdo_msg.Configuration.Fields(cdoSMTPAuthenticate) = cdoBasic
    cdo_msg.Configuration.Fields(cdoSMTPUseSSL) = True
    cdo_msg.Configuration.Fields(cdoSendUserName) = "xxxxxxxxxx@gmail.com"
    cdo_msg.Configuration.Fields(cdoSendPassword) = "ppppppppp"
    cdo_msg.Configuration.Fields.Update

    'remplissage et envoi message
    cdo_msg.To = "adresse1"
    cdo_msg.From = "adresse2"
    cdo_msg.Subject = "filename Sent to www.???.com "
    cdo_msg.TextBody = "File FTP LOG ATTACHED."
    cdo_msg.AddAttachment ("C:\Users\nnnnnn\Documents\classeur1.xls")
    cdo_msg.Send

    'libération objet message
    Set cdo_msg = Nothing
    End Sub


    1. Merci pour ton post, en l'adaptant ca fonctionne parfaitement bien, reste à customiser pour l'adapter au job.

      si ca peut aider :

       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

      Try
      Dim Smtp_Server As New SmtpClient
      Dim e_mail As New MailMessage()
      Smtp_Server.UseDefaultCredentials = False
      Smtp_Server.Credentials = New Net.NetworkCredential("titi.titi@xxxxxx.com", "password")
      Smtp_Server.Port = 587
      Smtp_Server.EnableSsl = True
      Smtp_Server.Host = "smtp.office365.com"

      e_mail = New MailMessage()
      e_mail.From = New MailAddress("titi.titi@xxxxxx.com")
      e_mail.To.Add("tata.tata@hotmail.fr, to.to@wanadoo.fr, nicolas.de_sousa@selhagroup.com")
      e_mail.Subject = "Email try Sending"
      e_mail.IsBodyHtml = False
      e_mail.Body = "ca marche : "
      Smtp_Server.Send(e_mail)
      MsgBox("Mail Sent")

      Catch error_t As Exception
      MsgBox(error_t.ToString)
      End Try

      End Sub