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

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

MailMessage.Attachments

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


MailMessage.Bcc

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


MailMessage.Body

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


MailMessage.BodyFormat

  

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><h1>This is a test message</h1><h2>This message should have HTML-type formatting</h2>Please use an HTML-capable viewer.";
      try
      {
         SmtpMail.Send(mm);
      } catch (System.Web.HttpException)
      {
         Console.WriteLine("This device is unable to send Internet messages");
      }
   }
}


MailMessage.From

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


MailMessage.Headers.Add

  

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><h1>This is a test message</h1><h2>This message should have HTML-type formatting</h2>Please use an HTML-capable viewer.";
      try
      {
         SmtpMail.Send(mm);
      } catch (System.Web.HttpException)
      {
         Console.WriteLine("This device is unable to send Internet messages");
      }
   }
}


MailMessage.Priority

  

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><h1>This is a test message</h1><h2>This message should have HTML-type formatting</h2>Please use an HTML-capable viewer.";
      try
      {
         SmtpMail.Send(mm);
      } catch (System.Web.HttpException)
      {
         Console.WriteLine("This device is unable to send Internet messages");
      }
   }
}


MailMessage.Subject

  

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


MailMessage.To

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