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 : 23274
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

{C# / C#.NET} CLASSE D'ENVOIE DE MAIL PAR SMTPCLIENT
Petite classe permettant d'envoyer un mail avec ou sans PJ. Dans l'exemple je prend un fichier ht...

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

{} REGEXS DE VÉRIFICATION D'E-MAIL, URL ET NUMÉRO DE TÉLÉPHONE
Ces regexs permettent de déterminer si le texte entré est un numéro de téléphone, un e-mail ou alors...

{JAVA / J2EE} CLIENT ET SERVEUR DE MESSAGERIES
Simple Application de messagerie avec Interface utilisateur. Vos disposerez de deux dossier l'un reg...

{Visual Basic, VB6, VB.NET, VB 2005} ENVOI DE MAIL AVEC PIÈCE JOINTE
Bonjour, Permet soit d'envoyer des mails "avec pièce jointe directement", soit d'envoyer des mail...

{PHP} SIMPLE EMAILING SERVICE
Voici un code que j'ai écris pour une de mes sites www.bonjourbuzz.com C'est assez simple à utilise...

{Visual Basic, VB6, VB.NET, VB 2005} EDITEUR DE BBCODE & ENVOI MAIL ANIMÉ PAR L'AGENT MERLIN LE MAGICIEN
J'ai trouvé un trés Bon HTA nommé Dingue_BBCode_Editor.0.6 : http://www.htasoft.com/ C'est un édit...

{PHP} FONCTION ENVOI DE MAIL PHP QUI PERMET DE PASSER LE FILTRE ANTI SPAM
Tout est dans le titre :) C'est une fonction qui vous permettra de passer outre les filtres antis...

{Visual Basic, VB6, VB.NET, VB 2005} ENVOI MAIL AVEC MERLIN LE MAGICIEN
C'est une Mise à jour du SMTP Mail Client avec pièce jointe ! - Description de la Mise à jour : ...

{Visual Basic, VB6, VB.NET, VB 2005} SMTP : PROJET COMPLET D'ENVOI DE COURRIEL, SANS UTILISATION DE DLL SPÉCIFIQUE AUTRE QUE MSCOMCTL.OCX
----------------------------------- Ce code est basé sur "OCX ENVOIE EMAIL VERSION 2" de pcpunch (...