SmtpClient: From, Subject, Body, Attachments, To : Mail « Network « C# / C Sharp






SmtpClient: From, Subject, Body, Attachments, To

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

    }
}

   
    
  








Related examples in the same category

1.Mail Test
2.A POP3 e-mail checkerA POP3 e-mail checker
3.Fancy Mail Test
4.Mail Attach Test
5.Send Email
6.Send HTML Mail
7.Is Valid Email
8.Is Valid Email Address
9.Is Email
10.Sends a MailMessage object using the SMTP settings.
11.Smtp Send