Отправка email - C# (239156)
Формулировка задачи:
Проблема в следующем не работает отправка прикрепленных файлов формата .rar, остальных работает именно .rar нет! Кто-нибудь сталкивался? Раньше писал на Delphi работало, на C# не хотит!
Код:
var smtp = new SmtpClient("smtp.mail.ru", 25);
smtp.Credentials = new NetworkCredential("Логин", "Пароль");
var message = new MailMessage();
message.From = new MailAddress("От кого");
message.To.Add(new MailAddress("Кому"));
message.Subject = "ТестЗаголовок";
message.Body = "ТестСообщение";
//Прикрепляем файл
var file = @"C:\test.rar";
var attach = new Attachment(file);
// Добавляем информацию для файла
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);
try
{
smtp.Send(message);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}Решение задачи: «Отправка email»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Net.Mail;
namespace rar_sending
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendMail("smtp.yandex.ru", 25, "твоя_почта@yandex.ru", "пароль", "откуда@yandex.ru", "кому@yandex.ru", textBox1.Text, "Письмо - with rar archive", true, "C:\\ABBL22072010.rar");
}
public static void SendMail(string host, int port, string userName, string pswd, string fromAddress, string toAddress, string body, string subject, bool sslEnabled, string fileName)
{
Attachment att = new Attachment("C:\\ABBL22072010.rar");
MailMessage msg = new MailMessage(new MailAddress(fromAddress), new MailAddress(toAddress));
msg.Subject = subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Attachments.Add(att);
SmtpClient client = new SmtpClient(host, port);
client.Credentials = new System.Net.NetworkCredential(userName, pswd);
client.EnableSsl = sslEnabled;
try
{
client.Send(msg);
MessageBox.Show("Your message was sent successfully.");
}
catch (SmtpException ex)
{
MessageBox.Show("There was an error sending your message. {0}", ex.Message);
}
}
}
}