Les Snippets

Connexion

Envoyer un mail

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 13/03/2008 11:48:21 et initié par danuz [Liste]
Vue : 11765
Catégorie(s) : Email & Messagerie
Langages dispo pour ce code :
- ASP.NET 2.x, C# 2.x
- PHP 3, PHP 4, PHP 5
- VB6, VBA



Langage : C# 2.x , ASP.NET 2.x
Date ajout : 13/03/2008
Posté par danuz [Liste]
private String m_Username = String.Empty; 
    private String m_Password = String.Empty; 
    private String m_SmtpServer = String.Empty; 
         
    /// <summary> 
    /// Value has been set by default at 25. Default value for SmtpServer 
    /// </summary> 
    private int m_Port = 25; 
 
 
/// <summary> 
    /// Sends an email with attachment 
    /// </summary> 
    /// <param name="_from">person who is sending the mail</param> 
    /// <param name="_to">person who will receive the mail</param> 
    /// <param name="_subject">mail subject</param> 
    /// <param name="_message">mail message</param> 
    /// <param name="attachment">file that will be attached to the mail</param> 
    /// <returns>True if mail haas been sent properly. false otherwise</returns> 
    public Boolean Send(String _from, String _to, String _subject, String _message, String attachment) 
    { 
            try { 
        MailAddress from = new MailAddress(_from); 
        MailAddress to = new MailAddress(_to); 
 
        MailMessage em = new MailMessage(from, to); 
                    //em.CC.Add(); It is also possible to add CC or BCC recipient on that way. 
                    //em.Bcc.Add(); 
                    em.BodyEncoding = Encoding.UTF8; 
                    em.IsBodyHtml = true; 
                    em.Subject = _subject; 
                    em.Body = _message; 
		 
                    if(!String.IsNullOrEmpty(attachment)) 
                    { 
                            Attachment a = new Attachment(attachment); 
            em.Attachments.Add(a); 
                    } 
 
                    return Send(em); 
            } 
            catch (Exception exc) { 
                    Trace.WriteLine(String.Format("Email could not be send.", exc)); 
                    return false; 
            } 
    } 
 
    /// <summary> 
    /// Sends a Mail (MailMessage) 
    /// </summary> 
    /// <param name="_message">mail message</param> 
    /// <returns>True if mail haas been sent properly. false otherwise</returns> 
    private Boolean Send(MailMessage _message) 
    { 
            try { 
                    if(String.IsNullOrEmpty(this.m_SmtpServer)) 
                            throw new Exception("SmtpServer must be specified"); 
 
        SmtpClient client = new SmtpClient(this.m_SmtpServer); 
                    client.Port = this.m_Port; 
 
                    if (this.m_Username != null && this.m_Password != null) { 
                            client.UseDefaultCredentials = false; 
                            client.Credentials = new NetworkCredential(this.m_Username, this.m_Password); 
                    } 
 
        client.Send(_message); 
                    return true; 
            } 
            catch (SmtpException exc) { 
                    Trace.WriteLine(String.Format("Email could not be send.", exc)); 
                    return false; 
            } 
    }
 
Remarque :
Les namespaces nécessaires : System, System.Diagnostics, System.Net.Mail, System.Text et System.Net.
Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 09/04/2008
Posté par nicomilville [Liste]
mail(destinataire, sujet, message, header);
Remarque :
Il faut que le server ai activé la fonction mail !!!
Langage : VB6 , VBA
Date ajout : 05/05/2008
Posté par mortalino [Liste]
Public Sub SendNotesMail(ByVal Subject As String, ByVal Attachment As String, _
                         ByVal Recipient As String, ByVal ccRecipient As String,  _
                         ByVal bccRecipient As String, ByVal BodyText As String, _
                         ByVal SaveIt As Boolean, ByVal Password As String)
                         
    Dim Maildb      As Object       'La base des  mails
    Dim UserName    As String       'Le nom  d'utilisateur
    Dim MailDbName  As String       'Le nom de la base des  mails
    Dim MailDoc     As Object       'Le  mail
    Dim AttachME    As Object       'L'objet pièce jointe  en RTF
    Dim Session     As Object       'La session  Notes
    Dim EmbedObj    As Object       'L'objet  incorporé
   
    'Crée une session  notes
    Set Session = CreateObject("Notes.NotesSession")
   
    '*** Cette ligne est  réservée aux versions 5.x et supérieur : ***
   ''  Session.Initialize (Password)
   
    'Récupère  le nom d'utilisateur et crée le nom de la base des mails
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1,  UserName, " "))) & ".nsf"
   
    'Ouvre la base des mails
    Set Maildb = Session.GETDATABASE("", MailDbName)
    If Not Maildb.IsOpen Then Maildb.OPENMAIL
       
    'Paramètre le mail à envoyer
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Recipient
    MailDoc.CopyTo = ccRecipient
    MailDoc.BlindCopyTo = bccRecipient
    MailDoc.Subject = Subject
    MailDoc.Body = BodyText
    MailDoc.SAVEMESSAGEONSEND = SaveIt
   
    'Prend en compte les pièces jointes
    If Attachment <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        MailDoc.CREATERICHTEXTITEM ("Attachment")
    End If
   
    'Envoie le  mail
    MailDoc.PostedDate = Now()
    MailDoc.SEND 0, Recipient
   
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
End Sub

Remarque :
Méthode pour Lotus Notes
(je ne sais plus si je l'ai trouvé dans un site ou dans une des sources de Jack)

Snippets en rapport avec : Mail, Mailmessage, Attachment



Codes sources en rapport avec : Mail, Mailmessage, Attachment

{Flash} FORMULAIRE MAIL
un petit exemple pour les gens qui ont besoin de créer un formulaire de contact par exemple pour le...

{PHP} MESSAGERIE INTERNE AVEC FICHIER TEXTE
Re voici le code php/html d'une petite messagerie interne, les mails sont conservés dans un fichier ...

{C# / C#.NET} ENVOYER UN EMAIL MULTIDESTINATAIRE EN SMTP AVEC ACCUSÉ DE LECTURE, CHOIX DES PIÈCES JOINTES, MOT DE PASSE CACHÉ
Programme permettant l'envoi de mails grâce au protocole SMTP, avec authentification de l'expéditeur...

{Flash} MAIL EN FLASH CS3 AS2 ET PHP
Scripts réalisés avec Flash CS3 en ActionScript 2 & PHP J'ai longtemps cherché un bout de code qu...

{Python} VÉRIFIER SES MAILS AVEC TUX DROID
Utilise libgmail pour télécharger les nouveaux mails Gmail par thread de discussion. Puis se connec...

{Visual Basic, VB6, VB.NET, VB 2005} ENVOI DE MAIL(MAILING ET/OU MAILBOMBING, ENVOI SYNCHRONE/ASYNCHRONE
Voila une petite source (ma première) qui permet d'envoyer un/des mail(s) a une/plusieurs personne(s...

{} [FLEX 4/AIR] ENVOYER UN MAIL DEPUIS UNE APPLICATION AIR EN SMTP
J'ai voulu envoyer un mail via AIR, mais visiblement il n'y a pas de class prévue à cet effet. Aprè...

{PHP} CLASSE DE VÉRIFICATION DE DONNÉES
cette classe réunies plusieurs vérifications de données que je fais souvent pour mes sites : vérific...

{Delphi} MAILS AVEC INDY10 : QUE FAIRE LORSQUE LE CONTENU AU FORMAT MIM N' EST PAS VISUALISÉ CORRECTEMENT
Bonjour à tous, suite à l' installation de mon programme "Mail Server" (qui gère la réception de...

{C# / C#.NET} SEND MAIL WITH GMAIL
voila un petit programme qui permet d'envoyer des mail en utilisant le client SMTP de GMAIL il manq...