Pull to refresh

Баги с отправкой почты.

Писал я однажды прогу для отправки почты через различные почтовые сервера.
Но когда начал пробовать посылать через рамблер, то возникла ошибка «The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required».
SSL включен.
Через аккаунт на гмаил всё работало ( для него тоже требуется ssl).

Код был следующий:
System.Net.Mail.MailMessage MainMessage= new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient Smtp = new SmtpClient();
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
Smtp.Host = "mail.rambler.ru";
Smtp.Port = 25;// так же пробовал 587 порт
Smtp.EnableSsl = true;
MainMessage.From = "alamar@rambler.ru;
string to = "alamar_13@mail.ru";
MainMessage.To.Add(new MailAddress(to));
MainMessage.Body = "body text";
MainMessage.Subject = "my subject";
Smtp.UseDefaultCredentials = false;
Smtp.Credentials = new System.Net.NetworkCredential("alamar@rambler.ru", "*****");
Smtp.Send(MainMessage);


Решение крылось в использовании System.Web.Mail.MailMessage
Так как в System.Net.Mail.MailMessage и System.Net.Mail.SmtpClient существует баг (от MS) при реализации RFC-2554.
А в System.Web.Mail.MailMessage этот баг исправили.
Надо добавить в references — system.web
Пример рабочего кода.
void SendMail()
{
System.Web.Mail.MailMessage m = new System.Web.Mail.MailMessage();
m.From = "alamar@rambler.ru";
m.To = "alamar.jf@gmail.com";
m.BodyFormat = System.Web.Mail.MailFormat.Text;
m.BodyEncoding = System.Text.Encoding.GetEncoding("windows-1251");
m.Subject = "Тема пиьсма";
m.Body = "текст письма";
// для аутентификации на SMTP сервере
m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "alamar@rambler.ru");
m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "XXXXXX"); //пароль
System.Web.Mail.SmtpMail.SmtpServer = "mail.rambler.ru";
System.Web.Mail.SmtpMail.Send(m);
}


Вот собственно и мой первый пост :)
Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.