Envoyer un email automatiquement

cs_faress1 Messages postés 17 Date d'inscription lundi 11 août 2008 Statut Membre Dernière intervention 3 septembre 2008 - 15 août 2008 à 17:41
gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 - 15 août 2008 à 18:19
Bonjour,


je vous demande comment faire pour envoyer un email d'une façon automatique tel que par exemple, l'aplication envoie un email "msg" a l'arriver d'une date.


merci d'avance.


 

1 réponse

gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
15 août 2008 à 18:19
Imports




System.Net.Mail



Imports




System.Text



Imports




System.ServiceProcess
 ' class machin

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)
ThenarlAttachments =

New ArrayList()

' Clear the "(No Attachments)" default text in the ListViewAttachments.Items.Clear()

End
IfarlAttachments.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
With

End
Sub

Private
Sub btnSend_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles btnSend.Click

Dim from
As
String =
"toi@gmail.com"

If tbTo.Text =
""
ThenMsgBox(

"You must enter Mail To addresses.")

Exit
Sub

End
If

' Use the StringBuilder class instead of traditional string concatenation.

' It is optimized for building strings because it is capable of modifying

' the underlying buffer instead of having to make a copy of the string for

' each concatenation.

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 SmtpClient(
"smtp.gmail.com", 587)client.Timeout = 10000
client.EnableSsl Trueclient.Credentials

New Net.NetworkCredential(
"toi@gmail.com",
"tonmotdepasse",
"")
' Username does not have @gmail.com

' 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)

cleanup()

Catch exp
As ExceptionMessageBox.Show(

"The following problem occurred when attempting to " & _

"send your email: " & exp.Message, _

Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

End
Try

End
Sub
'certaines données sont à modifier

Bonjour chez vous !
0
Rejoignez-nous