2010-05-03 67 views
0

我開發C#中的簡單的發送郵件的應用程序,使用我的CMail服務器:驗證錯誤發送郵件應用

MailMessage mail = new MailMessage("[email protected]", "[email protected]"); 
     mail.Subject = "Sub"; 
     mail.Body = "Hi!"; 
     SmtpClient smtp = new SmtpClient("MyServer"); 
     System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "pass"); 
     smtp.UseDefaultCredentials = false; 
     smtp.Credentials = cred; 
     smtp.Send(mail); 

很顯然,我中省略我的帳戶信息,因此,此代碼拋出我的身份驗證異常因爲某些原因。我首先認爲代碼是錯誤的,所以我將信息更改爲我的Gmail帳戶,並且一切正常,只有我遇到問題的SMTP服務器與CMail一起。 .NET和CMail的SMTP有問題嗎?

感謝您的幫助和意見!

回答

0

嘗試增加:

smtp.EnableSsl = true; 
+0

不,我已經嘗試過,但不起作用。 – doc 2010-05-03 20:50:10

0

如果您使用兩步驗證,那麼你將需要添加專用密碼。

0

全部工作樣本

public static void sendEmail() 
    { 
     //for use GMAIL require enable - 
     //https://myaccount.google.com/lesssecureapps?rfn=27&rfnc=1&eid=39511352899709300&et=0&asae=2&pli=1 
     Console.WriteLine("START MAIL SENDER"); 

     //Авторизация на SMTP сервере 
     SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587); 
     Smtp.EnableSsl = true; 
     Smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
     Smtp.UseDefaultCredentials = false; 
     //username password 
     Smtp.Credentials = new NetworkCredential("rere", "rere"); 

     //Формирование письма 
     MailMessage Message = new MailMessage(); 
     Message.From = new MailAddress("[email protected]"); 
     Message.To.Add(new MailAddress("[email protected]")); 
     Message.Subject = "test mesage"; 
     Message.Body = "tttt body"; 

     string file = "D:\\0.txt"; 
     if (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); 
      Console.WriteLine("ADD FILE [" + file + "]"); 
     } 
     try 
     { 
      Smtp.Send(Message); 
      MessageBox.Show("SUCCESS"); 
     } 
     catch { MessageBox.Show("WRONG"); } 
    }