Publié le 14/02/2008 par Another
Un exemple type d'implémentation de la classe Mail du namespace System.Web.Mail du Framework. Cette exemple permet de définir les options les plus courantes : Priorité des mails, Pièces jointes, SSL, numéro de port...
private string sendMail() { MailMessage mail = new MailMessage(); mail.From = this.from; mail.To = this.to; mail.Cc = this.cc; mail.Bcc = this.bcc; if (this.highPriority == true) { mail.Priority = MailPriority.High; } mail.Subject = this.subject); if (this.body.StartsWith("<") == false) { mail.BodyFormat = MailFormat.Text; } else { mail.BodyFormat = MailFormat.Html; } mail.Body = this.body; if (this.attachment != "") { string[] files = attachment.Split('|'); int count = files.Length; for (int i = 0; i < count; i++) { MailAttachment ma = new MailAttachment(files[i], MailEncoding.Base64); mail.Attachments.Add(ma); } } string cdo = "http://schemas.microsoft.com/cdo/configuration"; // Handle SMTP authentication if (this.smtpUsername != "") { if (this.smtpPassword != "") { mail.Fields.Add(cdo + "/smtpauthenticate", "1"); mail.Fields.Add(cdo + "/sendusername", this.smtpUsername); mail.Fields.Add(cdo + "/sendpassword", this.smtpPassword); } } // Handle SMTP configuration if (this.smtpPort != "") { mail.Fields.Add("/smtpserverport", this.smtpPort); } if (this.smtpUseSsl == true) { mail.Fields.Add("/smtpusessl", this.smtpUseSsl); } if (this.smtpTimeout != "") { mail.Fields.Add("/smtpconnectiontimeout", this.smtpTimeout); } // Actually send this mail SmtpMail.SmtpServer = this.smtpServer; try { SmtpMail.Send(mail); this.errorMessage = ""; } catch (Exception ex) { this.errorMessage = ex.Message; } return (this.errorMessage); }