C# Programmation Réseau: Envoyer un fichier par email

Author:
 
using System;
using System.Net;
using System.Net.Mail;

class MainClass {
    public static void Main(string[] args) {
        SmtpClient client = new SmtpClient("mail.serversmtp.com", 25);
        client.Credentials = new NetworkCredential("user@mesexemple.com", "password");
        using (MailMessage msg = new MailMessage()) {
            msg.From = new MailAddress("admin@mesexemples.com");
            msg.Subject = "Welcom";
            msg.Body = "Bienvenu sur CSharp.";
            // Liste des fichiers à attacher à l'email
            msg.Attachments.Add(new Attachment("fichier.css", "text/plain"));
            msg.Attachments.Add(new Attachment("allo.doc", "application/octet-stream"));

            foreach (string str in args) {
                try {
                    msg.To.Add(new MailAddress(str));
                } catch (FormatException ex) {
                    Console.WriteLine("{0}: Error -- {1}", str, ex.Message);
                    continue;
                }
            }
            client.Send(msg);
        }

    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *