Envoi de mail(mailing et/ou mailbombing, envoi synchrone/asynchrone

Soyez le premier à donner votre avis sur cette source.

Snippet vu 6 797 fois - Téléchargée 18 fois

Contenu du snippet

Voila une petite source (ma première) qui permet d'envoyer un/des mail(s) a une/plusieurs personne(s). Elle prend en charge l'envoi asynchrone des données et la synchronisation des threads. Toute critique est la bienvenue surtout négative :) Il est possible que j'ai oublié des trucs car je l'ai fait ce matin mêmef en 30 min.

Source / Exemple :


Imports System.Net.Mail
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Threading
Imports System.ComponentModel

<Serializable(), StructLayout(LayoutKind.Auto)> _
Public Class Mail

    <Serializable()> _
    Public Class MailMessage

        Private Mail_Src As String ' Adresse source
        Private Mail_Dest As List(Of String) 'Adresse(s) de destination
        Private Mail_Nbr_Mail_By_Dest As ULong ' Nombre de message par personne
        Private Mail_Subject As String 'Sujet
        Private Mail_Body As String ' Corps
        Private Mail_Attachment_Path As String 'Fichier d'attache
        Private Mail_Is_Html As Boolean ' Si le mail est en html
        Private Mail_Smtp_Addr As String 'Adresse du serveur smtp

        Public Sub New()

        End Sub

        Public Sub New(ByVal MailMsg As MailMessage)

            Mail_Src = MailMsg.Mail_Src
            Mail_Dest = MailMsg.Mail_Dest
            Mail_Nbr_Mail_By_Dest = MailMsg.Mail_Nbr_Mail_By_Dest
            Mail_Subject = MailMsg.Mail_Subject
            Mail_Body = MailMsg.Mail_Body
            Mail_Attachment_Path = MailMsg.Mail_Attachment_Path
            Mail_Is_Html = MailMsg.Mail_Is_Html
            Mail_Smtp_Addr = MailMsg.Mail_Smtp_Addr

        End Sub

        Public Sub New(ByVal Src As String, ByVal Dest As List(Of String), _
        ByVal NbrMailDest As ULong, ByVal Subject As String, ByVal Body As String, _
        ByVal Attachment As String, ByVal IsHtml As Boolean, ByVal SmtpAddr As String)

            Mail_Src = Src
            Mail_Dest = Dest
            Mail_Nbr_Mail_By_Dest = NbrMailDest
            Mail_Subject = Subject
            Mail_Body = Body
            Mail_Attachment_Path = Attachment
            Mail_Is_Html = IsHtml
            Mail_Smtp_Addr = SmtpAddr

        End Sub

        Public Property GetSrc() As String
            Set(ByVal value As String)
                Mail_Src = value
            End Set
            Get
                Return Mail_Src
            End Get
        End Property

        Public Property GetNbrMailByDest() As ULong
            Set(ByVal value As ULong)
                Mail_Nbr_Mail_By_Dest = value
            End Set
            Get
                Return Mail_Nbr_Mail_By_Dest
            End Get
        End Property

        Public Property GetDests() As List(Of String)
            Set(ByVal value As List(Of String))
                Mail_Dest = value
            End Set
            Get
                Return Mail_Dest
            End Get
        End Property

        Public Property GetSubject() As String
            Set(ByVal value As String)
                Mail_Subject = value
            End Set
            Get
                Return Mail_Subject
            End Get
        End Property

        Public Property GetBody() As String
            Set(ByVal value As String)
                Mail_Body = value
            End Set
            Get
                Return Mail_Body
            End Get
        End Property

        Public Property GetAttachmentPath() As String
            Set(ByVal value As String)
                Mail_Attachment_Path = value
            End Set
            Get
                Return Mail_Attachment_Path
            End Get
        End Property

        Public Property GetIsHtml() As Boolean
            Set(ByVal value As Boolean)
                Mail_Is_Html = value
            End Set
            Get
                Return Mail_Is_Html
            End Get
        End Property

        Public Property GetSmtpAddr() As String
            Set(ByVal value As String)
                Mail_Smtp_Addr = value
            End Set
            Get
                Return Mail_Smtp_Addr
            End Get
        End Property

    End Class

    Private MailMsg As MailMessage
    Private SendingResult As IAsyncResult
    Private Canceled As Boolean = False
    Public Shared IsActive As Boolean = False
    Private Delegate Sub InvokeSend()
    Private DelegSend As InvokeSend
    Private NbrMail As ULong
    Private NbrMailsSent As ULong = 1
    Public Event Message_Sent As EventHandler
    Public Event Sending_Error As EventHandler
    Public Event MailAddressSent As EventHandler

    Public Sub New(ByVal Mail_Msg As MailMessage)

        MailMsg = Mail_Msg
        If MailMsg.GetDests.Count = 1 Then
            'Calcule le nombre de messages total à envoyer
            NbrMail = MailMsg.GetNbrMailByDest
        Else
            'Calcule le nombre de messages total à envoyer
            NbrMail = (MailMsg.GetNbrMailByDest * MailMsg.GetDests.Count - 1)
        End If

    End Sub

    Public ReadOnly Property GetNbrMail() As ULong

        Get
            Return NbrMail
        End Get

    End Property

    Public ReadOnly Property GetCanceled() As Boolean

        Get
            Return Canceled
        End Get

    End Property

    Public WriteOnly Property SetCanceled() As Boolean

        Set(ByVal value As Boolean)
            Canceled = value
        End Set

    End Property

    Public ReadOnly Property GetIsActive() As Boolean

        Get
            Return IsActive
        End Get

    End Property

    <MethodImpl(MethodImplOptions.Synchronized)> _
    Public Function Send() As Boolean

        IsActive = True
        Try
            For Each Addr_Dest As String In MailMsg.GetDests
                RaiseEvent MailAddressSent(DirectCast(Addr_Dest, Object), EventArgs.Empty)
                Dim Mail As New System.Net.Mail.MailMessage
                Mail.From = New MailAddress(MailMsg.GetSrc)
                Mail.To.Clear()
                Mail.To.Add(Addr_Dest)
                Mail.Body = MailMsg.GetBody
                Mail.Subject = MailMsg.GetSubject
                Mail.IsBodyHtml = MailMsg.GetIsHtml
                Dim Mail_Smtp As New SmtpClient
                Mail_Smtp.Host = MailMsg.GetSmtpAddr
                For Nbr_Msg_Sending As ULong = 0 To MailMsg.GetNbrMailByDest
                    If Canceled Then
                        IsActive = False
                        Return False
                    End If
                    Mail_Smtp.Send(Mail)
                    RaiseEvent Message_Sent(NbrMailsSent, EventArgs.Empty)
                    NbrMailsSent += 1
                Next Nbr_Msg_Sending
            Next
            IsActive = False
            Return True
        Catch ex As Exception
            RaiseEvent Sending_Error(DirectCast(ex, Object), EventArgs.Empty)
            Canceled = True
            IsActive = False
            Return False
        Finally
        End Try

    End Function

    Public Sub BeginSend()

        DelegSend = New InvokeSend(AddressOf Send)
        SendingResult = DelegSend.BeginInvoke(Nothing, Nothing)

    End Sub

    Public Function EndSend() As Boolean

        If Canceled OrElse DelegSend IsNot Nothing Then
            Return Nothing
        Else
            DelegSend.EndInvoke(SendingResult)
        End If

    End Function

    Public Sub CancelSending()

        If Not Canceled AndAlso DelegSend IsNot Nothing Then
            Canceled = True
            DelegSend.EndInvoke(SendingResult)
        End If

    End Sub

    REM Verie si une adresse email est conforme syntaxiquement
    Public Shared Function IsValidMail(ByVal AddrMail As String) As Boolean

        Dim RegVerify As New Regex("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
        Return RegVerify.IsMatch(AddrMail)

    End Function

End Class

A voir également

Ajouter un commentaire Commentaires
Messages postés
1
Date d'inscription
samedi 14 janvier 2006
Statut
Membre
Dernière intervention
13 septembre 2009

J V LESSAI ET MERCI
Messages postés
3
Date d'inscription
jeudi 27 mars 2008
Statut
Membre
Dernière intervention
22 avril 2009

merci
g vé l'essai

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.