⌘BABAKOV.NET Let's code!
100% [light] [ru]

Example of Sending Email in C#

Simple way to send email using standard means.

To do this, we need to connect libraries:

using System.Net;
using System.Net.Mail;
using System.Net.Mime;


The code for sending a letter with an attachment:

//Указываем SMTP сервер и авторизуемся.
SmtpClient Smtp_Client = new SmtpClient("почтовый сервер", порт);
Smtp_Client.Credentials = new NetworkCredential("пользователь", "пароль");
//Выключаем или включаем SSL - (например для гугла должен быть включен).
Smtp_Client.EnableSsl = false;

//Приступаем к формированию самого письма
MailMessage Message = new MailMessage();
Message.From = new MailAddress("ot_kogo@mail.ру");
Message.To.Add(new MailAddress("komu@mail.ру"));
Message.Subject = "Тема сообщения";
Message.Body = "Само сообщение";

//Теперь прикрепим файл к сообщению...
                
string file = "полный путь до файла";
Attachment attach = new Attachment(file, MediaTypeNames.Application.Octet);
            
// Добавляем информацию для файла
ContentDisposition disposition = attach.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

Message.Attachments.Add(attach);
                
Smtp_Client.Send(Message);//непосредственно само отправление...


That's it. The letter has been sent!

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.