验证错误发送邮件应用

问题描述:

我开发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有问题吗?

感谢您的帮助和意见!

尝试增加:

smtp.EnableSsl = true; 
+0

不,我已经尝试过,但不起作用。 – doc 2010-05-03 20:50:10

如果您使用两步验证,那么你将需要添加专用密码。

全部工作样本

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"); } 
    }