Comment faire pour envoyer un mail

johann36 Messages postés 130 Date d'inscription dimanche 15 février 2004 Statut Membre Dernière intervention 9 mai 2017 - 3 sept. 2008 à 16:20
gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 - 3 sept. 2008 à 23:57
Bonjour,

Des problèmes j'en ai et en veux-tu en voilà. Je commence à désespérer je me dis que je vais peut être vous donner le cahier des charges et vous demandez de me faire l'appli à ce tarif là.
Je vais quand même résisté et pour à nouveau enrichir mes faible connaissances en .Net je vais poser ma question.

Je souhaiterai savoir comment faire pour envoyer un mail avec vb.net ?

Le code que j'ai mis est le suivant mais je me retrouve avec des erreurs :

        Try
            Dim SendFrom As MailAddress = New MailAddress("toto@vbfrance.com")
            Dim SendTo As MailAddress = New MailAddress("titi@vbfrance.com")

            Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

            MyMessage.Subject = "Test email"
            MyMessage.Body = "OK"

            Dim emailClient As New SmtpClient("smtp.vbfrance.com")
            emailClient.Send(MyMessage)

            MsgBox("Votre message a été envoyé avec succès.")
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try

Avez-vous une idée à mon problème ?

Merci d'avance.

Johann36

3 réponses

gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
3 sept. 2008 à 17:02
Imports




System.Net.Mail



Imports




System.Text



Imports




System.ServiceProcess



Dim




arlAttachments


As

ArrayList



Private








Sub

btnAttach_Click(


ByVal

sender


As

System.Object,


ByVal

e


As

System.EventArgs)


Handles

btnAttach.Click



Dim

odlgAttachment


As





New

OpenFileDialog



With

odlgAttachment.InitialDirectory =



"C:"


.Filter =





"All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt"


.FilterIndex = 1








' The OpenFileDialog control only has an Open button, not an OK button.








' However, there is no DialogResult.Open enum so use DialogResult.OK.








If

.ShowDialog() = Windows.Forms.DialogResult.OK


Then








If

IsNothing(arlAttachments)


Then


arlAttachments =





New

ArrayList()



' Clear the "(No Attachments)" default text in the ListView


Attachments.Items.Clear()








End





If


arlAttachments.Add(





New

Attachment(.FileName))



' You only want to show the file name. The OpenFileDialog.FileName








' property contains the full path. So Split the path and reverse it








' to grab the first string in the array, which is just the FileName.








Dim

strFileName()


As





String

= .FileName.Split(


New





Char

() {


CChar

(


""

)})System.Array.Reverse(strFileName)

Attachments.Items.Add(strFileName(0))



End





If








End





WithEnd







SubPrivate







Sub

btnSend_Click(


ByVal

sender


As

System.Object,


ByVal

e


As

System.EventArgs)


Handles

btnSend.Click



Dim

from


As





String

=


"blabla@mail.com"








If

tbTo.Text =


""





Then


MsgBox(





"You must enter Mail To addresses."

)



Exit





Sub








End





If 










Dim

sb


As





New

StringBuilder()



' Build the email message body.


sb.Append(Trim(tbBody.Text))


sb.Append(vbCrLf)








' Creating a mail message is as simple as instantiating a class and








' setting a few properties.








Dim

mailMsg


As





New

MailMessage(from, tbTo.Text.Trim)



With

mailMsg.Subject = tbSubject.Text.Trim

.Body = sb.ToString



If





Not

IsNothing(arlAttachments)


Then








Dim

mailAttachment


As

Attachment



For





Each

mailAttachment


In

arlAttachments.Attachments.Add(mailAttachment)



Next








End





If








End





With








' Set the SmtpServer name. This can be any of the following depending on








' your local security settings:








' a) Local IP Address (assuming your local machine's SMTP server has the








' right to send messages through a local firewall (if present).








' b) 127.0.0.1 the loopback of the local machine.








' c) "smarthost" or the name or the IP address of the exchange server you








' utilize for messaging. This is usually what is needed if you are behind








' a corporate firewall.








' Use structured error handling to attempt to send the email message and








' provide feedback to the user about the success or failure of their








' attempt.








Try








Dim

client


As





New

Net.Mail.SmtpClient(


"smtp.mail.com"

, 587)client.Timeout = 10000

client.EnableSsl =



True


client.Credentials =





New

Net.NetworkCredential(


"blabla286"

,


"Y5p-§89"

,


""

)



' client.Send(Message)


client.Send(mailMsg)


Attachments.Items.Clear()


Attachments.Items.Add(





"(No Attachments)"

)MessageBox.Show(



"Your email has been successfully sent!"

, _



"Email Send Status"

, MessageBoxButtons.OK, _MessageBoxIcon.Information)



Catch

exp


As

ExceptionMessageBox.Show(



"The following problem occurred when attempting to "

& _



"send your email: "

& exp.Message, _



Me

.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)



End





TryEnd







Sub







Bonjour chez vous !
0
johann36 Messages postés 130 Date d'inscription dimanche 15 février 2004 Statut Membre Dernière intervention 9 mai 2017
3 sept. 2008 à 21:09
Décidément tu es partout, non loint de là une critique mais je pense que dans l'avenir il vaudra mieux que je te pose la question directement.
En tout cas merci pour l'info je l'essaierai demain matin, je te tiendrai informé sur le résultat.

Merci d'avance.

Johann36
0
gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
3 sept. 2008 à 23:57
non pose tes questions sur  le forum c'est fait pour cela
pense à activer le bouton réponse accptée si la réponse convient


 










Bonjour chez vous !
0
Rejoignez-nous