Csharp/C Sharp by API/System.Net.Mail/MailMessage

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

MailMessage.Attachments

<source lang="csharp"> using System; using System.Net; using System.Net.Mail; class MainClass {

   public static void Main(string[] args)
   {
       SmtpClient client = new SmtpClient("mail.somecompany.ru", 25);
       client.Credentials =new NetworkCredential("user@somecompany.ru", "password");
       using (MailMessage msg = new MailMessage())
       {
           msg.From = new MailAddress("author@aaa.ru");
           msg.Subject = "HI";
           msg.Body = "A message";
           msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
           msg.Attachments.Add(new Attachment("C:\\test.exe", "application/octet-stream"));
           msg.To.Add(new MailAddress("message to address"));
           client.Send(msg);
       }
   }

}


 </source>


MailMessage.Bcc

<source lang="csharp"> using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; public class MainClass {

   public static void Main()
   {
       SmtpClient client = new SmtpClient("smtp.localhost.ru");
       client.Credentials =new NetworkCredential("username", "password");
       MailMessage message = new MailMessage(new MailAddress("joe@net.ru", "Joe"),    // To
           new MailAddress("raj@web.ru", "Ray")); // CC
       message.Bcc.Add(new MailAddress("a@asdf.ru"));
       message.Bcc.Add(new MailAddress("m@fda.ru"));
       message.Subject = "subject";
       message.Body = "body";
       Attachment att = new Attachment(@"c:\file.cs");
       message.Attachments.Add(att);
       client.Send(message);
   }

}


 </source>


MailMessage.Body

<source lang="csharp"> using System; using System.Net; using System.Net.Mail; class MainClass {

   public static void Main(string[] args)
   {
       SmtpClient client = new SmtpClient("mail.somecompany.ru", 25);
       client.Credentials =new NetworkCredential("user@somecompany.ru", "password");
       using (MailMessage msg = new MailMessage())
       {
           msg.From = new MailAddress("author@aaa.ru");
           msg.Subject = "HI";
           msg.Body = "A message";
           msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
           msg.Attachments.Add(new Attachment("C:\\test.exe", "application/octet-stream"));
           msg.To.Add(new MailAddress("message to address"));
           client.Send(msg);
       }
   }

}


 </source>


MailMessage.BodyFormat

<source lang="csharp">

using System; using System.Web.Mail; public class FancyMailTest {

  public static void Main()
  {
     MailMessage mm = new MailMessage();
     mm.From = "haley@myisp.net";
     mm.To = "riley@yourisp.net;rich@shadrach.ispnet1.net";
     mm.Cc = "matthew@anotherisp.net;chris@hisisp.net";
     mm.Bcc = "katie@herisp.net;jessica@herisp.net";
     mm.Subject = "This is a fancy test message";
     mm.Headers.Add("Reply-To", "haley@myisp.net");
     mm.Headers.Add("Comments", "This is a test HTML message");
     mm.Priority = MailPriority.High;
     mm.BodyFormat = MailFormat.Html;
mm.Body = "<html><body>

This is a test message

This message should have HTML-type formatting

Please use an HTML-capable viewer.";
     try
     {
        SmtpMail.Send(mm);
     } catch (System.Web.HttpException)
     {
        Console.WriteLine("This device is unable to send Internet messages");
     }
  }

}


 </source>


MailMessage.From

<source lang="csharp"> using System; using System.Net; using System.Net.Mail; class MainClass {

   public static void Main(string[] args)
   {
       SmtpClient client = new SmtpClient("mail.somecompany.ru", 25);
       client.Credentials =new NetworkCredential("user@somecompany.ru", "password");
       using (MailMessage msg = new MailMessage())
       {
           msg.From = new MailAddress("author@aaa.ru");
           msg.Subject = "HI";
           msg.Body = "A message";
           msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
           msg.Attachments.Add(new Attachment("C:\\test.exe", "application/octet-stream"));
           msg.To.Add(new MailAddress("message to address"));
           client.Send(msg);
       }
   }

}


 </source>


MailMessage.Headers.Add

<source lang="csharp">

using System; using System.Web.Mail; public class FancyMailTest {

  public static void Main()
  {
     MailMessage mm = new MailMessage();
     mm.From = "haley@myisp.net";
     mm.To = "riley@yourisp.net;rich@shadrach.ispnet1.net";
     mm.Cc = "matthew@anotherisp.net;chris@hisisp.net";
     mm.Bcc = "katie@herisp.net;jessica@herisp.net";
     mm.Subject = "This is a fancy test message";
     mm.Headers.Add("Reply-To", "haley@myisp.net");
     mm.Headers.Add("Comments", "This is a test HTML message");
     mm.Priority = MailPriority.High;
     mm.BodyFormat = MailFormat.Html;
mm.Body = "<html><body>

This is a test message

This message should have HTML-type formatting

Please use an HTML-capable viewer.";
     try
     {
        SmtpMail.Send(mm);
     } catch (System.Web.HttpException)
     {
        Console.WriteLine("This device is unable to send Internet messages");
     }
  }

}


 </source>


MailMessage.Priority

<source lang="csharp">

using System; using System.Web.Mail; public class FancyMailTest {

  public static void Main()
  {
     MailMessage mm = new MailMessage();
     mm.From = "haley@myisp.net";
     mm.To = "riley@yourisp.net;rich@shadrach.ispnet1.net";
     mm.Cc = "matthew@anotherisp.net;chris@hisisp.net";
     mm.Bcc = "katie@herisp.net;jessica@herisp.net";
     mm.Subject = "This is a fancy test message";
     mm.Headers.Add("Reply-To", "haley@myisp.net");
     mm.Headers.Add("Comments", "This is a test HTML message");
     mm.Priority = MailPriority.High;
     mm.BodyFormat = MailFormat.Html;
mm.Body = "<html><body>

This is a test message

This message should have HTML-type formatting

Please use an HTML-capable viewer.";
     try
     {
        SmtpMail.Send(mm);
     } catch (System.Web.HttpException)
     {
        Console.WriteLine("This device is unable to send Internet messages");
     }
  }

}


 </source>


MailMessage.Subject

<source lang="csharp">

using System; using System.Net; using System.Net.Mail; class MainClass {

   public static void Main(string[] args)
   {
       SmtpClient client = new SmtpClient("mail.somecompany.ru", 25);
       client.Credentials =new NetworkCredential("user@somecompany.ru", "password");
       using (MailMessage msg = new MailMessage())
       {
           msg.From = new MailAddress("author@aaa.ru");
           msg.Subject = "HI";
           msg.Body = "A message";
           msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
           msg.Attachments.Add(new Attachment("C:\\test.exe", "application/octet-stream"));
           msg.To.Add(new MailAddress("message to address"));
           client.Send(msg);
       }
   }

}


 </source>


MailMessage.To

<source lang="csharp"> using System; using System.Net; using System.Net.Mail; class MainClass {

   public static void Main(string[] args)
   {
       SmtpClient client = new SmtpClient("mail.somecompany.ru", 25);
       client.Credentials =new NetworkCredential("user@somecompany.ru", "password");
       using (MailMessage msg = new MailMessage())
       {
           msg.From = new MailAddress("author@aaa.ru");
           msg.Subject = "HI";
           msg.Body = "A message";
           msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
           msg.Attachments.Add(new Attachment("C:\\test.exe", "application/octet-stream"));
           msg.To.Add(new MailAddress("message to address"));
           client.Send(msg);
       }
   }

}


 </source>