SmtpClient: From, Subject, Body, Attachments, To

image_pdfimage_print
   
 
using System;
using System.Net;
using System.Net.Mail;

class MainClass {
    public static void Main(string[] args) {
        SmtpClient client = new SmtpClient("mail.somecompany.com", 25);
        client.Credentials = new NetworkCredential("user@somecompany.com", "password");
        using (MailMessage msg = new MailMessage()) {
            msg.From = new MailAddress("author@visual-csharp-recipes.com");
            msg.Subject = "Greetings";
            msg.Body = "This is a  message.";

            msg.Attachments.Add(new Attachment("7.cs", "text/plain"));
            msg.Attachments.Add(new Attachment("7.exe", "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);
        }

    }
}

    


This entry was posted in C# Network. Bookmark the permalink.