Как отправить электронную почту на gmail с помощью SMTPclient на С#?

Я использую Outlook 2003 и Visual Studio 2008. Я хочу разработать приложение, которое будет отправлять электронную почту на любой домен. но мой код дает сбой, когда я пытаюсь отправить электронное письмо на gmail, hotmail и т. д. на самом деле все сообщения хранятся в каталоге C:\Inetpub\mailroot\Queue. Пожалуйста, помогите мне, как отправить электронное письмо на gmail, hotmail a/c.

Заранее спасибо

Код

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.To.Add("[email protected]");            
message.Subject = "This is sample mail";
message.From = new System.Net.Mail.MailAddress("[email protected]");
message.Body = "this is the message body";


System.Net.Mail.SmtpClient sss = new System.Net.Mail.SmtpClient("HO-KKJ-MAIL.in.niit.com");
sss.UseDefaultCredentials = false;
sss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sss.Credentials = new System.Net.NetworkCredential("Sumit.Dhingrar", "password","domain");

person sumit    schedule 18.04.2011    source источник
comment
Верны ли ваши учетные данные? Вы получаете сообщение об ошибке?   -  person Mizipzor    schedule 18.04.2011


Ответы (1)


Это хороший пример отправки электронной почты с помощью Gmail в C#.

string from = [email protected]; //Replace this with your own correct Gmail Address

string to = [email protected] //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");

client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
       try
        {
            client.Send(mail);
        }
        catch (Exception ex) 
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty; 
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
   HttpContext.Current.Response.Write(errorMessage );
        } // end try 

Уверены ли вы

message.From = new System.Net.Mail.MailAddress("[email protected]");

правильно? Есть ли у этого метода такая перегрузка?

person Soner Gönül    schedule 18.04.2011