Envoyer un mail

Contenu du snippet

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; 
            } 
    }
 

Compatibilité : ASP.NET 2.x, C# 2.x

Disponible dans d'autres langages :

A voir également

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.