Csharp/CSharp Tutorial/Network/EMail

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

Email: to, cc, bcc, subject, body and attachment

<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>

Send an email out

<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>