Отослать email - C#

Узнай цену своей работы

Формулировка задачи:

Вопрос не в том, чтобы просто отослать текст, но и таблицы, файлы (ну и форматировка) В общем нужно отсылать не только тривиальный текст.

Решение задачи: «Отослать email»

textual
Листинг программы
private void SendMail(string smtpServer, string port, string userName, string password, string mailSender,
            string senderName, string mailSubject, string mailBody, string mailReciver, bool useSsl = true, string attachmentName = "")
        {
            var client = new SmtpClient(smtpServer, Convert.ToInt32(port))
                {
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(userName, password),
                    EnableSsl = useSsl
                };
            var message = new MailMessage();
            message.From = new MailAddress(mailSender, senderName);
            message.Subject = mailSubject;
            message.Body = mailBody;
            if (mailBody.Contains("<html>") || mailBody.Contains("<tr") || mailBody.Contains("<table"))
                message.IsBodyHtml = true;
            message.To.Add(mailReciver);
            if (!string.IsNullOrEmpty(attachmentName))
            {
                string[] attachmentArr = attachmentName.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries);
                foreach (var attach in attachmentArr)
                {
                    var data = new Attachment(attach, MediaTypeNames.Application.Octet);
                    ContentDisposition disposition = data.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(attach);
                    disposition.ModificationDate = File.GetLastWriteTime(attach);
                    disposition.ReadDate = File.GetLastAccessTime(attach);
                    message.Attachments.Add(data);
                }
            }
            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                   MessageBox.Show(ex.ToString());
            }
        }

Оцени полезность:

11   голосов , оценка 4 из 5
Похожие ответы