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 : 2432
Catégorie(s) : Email & Messagerie
Langage sélectionné : C# 2.x
Langages dispo pour ce code :
- ASP.NET 2.x, C# 2.x
- PHP 3, PHP 4, PHP 5
- VB6, VBA
- Voir tous les langages pour ce code snippet



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.

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...

{Delphi} MAIL INDY10: MULTIPART/RELATED MESSAGEPARTS
Pour les 2 ou 3 personnes que ça va interesser vu le succès mondial de mes sources sur les mails ave...

{C# / C#.NET} CRÉATION ET ENVOI DE FICHIERS ZIPPÉS
Ce petit prog fait en wpf permet de zipper le contenu d'un dossier en excluant les fichiers binaires...

{Visual Basic, VB6, VB.NET, VB 2005} FONCTION ENVOI DE MAIL PAR VBA SANS CLIENT LOCAL, PAR SERVEUR SMTP
Cette fonction est simple, courte, facile à utiliser, elle ne nécessite pas de Outlook ou autre Lotu...

{Visual Basic, VB6, VB.NET, VB 2005} NEWSLETTER MESSAGERIE
Cette application nous permet gérer une Newsletter en vb.net, en relation avec une base de donnée sq...

{Visual Basic, VB6, VB.NET, VB 2005} GESTION DES FICHIERS DE WINDOWS MAIL SOUS VISTA
Sauver et restaurer les courriers de Windows Mail,son carnet d'adresses et les favoris de IE 7, le t...

{Delphi} MAILS AVEC INDY10 : CONTENTTYPE/CONTENTID/PARENTPART
Mails avec Indy : contentType/ContentID/ParentPart Note: Ce tuto n' est pas fait pour vous appren...

{Visual Basic, VB6, VB.NET, VB 2005} GESTION DE NEWSLETTER (PLUGIN OUTLOOK)
Ce petit programme permet de générer un envoi de newsletter dans Outlook à partir d'un fichier csv (...

{Delphi} MAILS AVEC INDY:QUE FAIRE (ET POURQUOI) LORSQUE L' ANEXE NE SEMBLE PAS AVOIR DE NOM (FILENAME='')
Bonjour à tous!!! ok, ça fait longtemps que je ne participe pas mais je vous apporte aujourd' hui u...

{C / C++ / C++.NET} SCANNER D'ADRESSES MAILS PRÉSENTENT SUR GOOGLE
Cette source a déjà été programmée en python http://www.pythonfrance.com/codes/SCANNER-ADRESSES-MAIL...