2010-12-17 76 views
1

发送电子邮件时,我收到以下错误:发送电子邮件失败,在C#3.5

The device is not ready at System.Net.Mail.SmtpClient.Send(MailMessage message).

的代码是:

MailMessage mailMessage = new MailMessage(senderEmail, cleanRecipients) 
{ 
    Subject = string.empty, 
    Body = string.empty, 
    IsBodyHtml = false 
}; 
SmtpClient smtpClient = new SmtpClient(); 
smtpClient.Send(mailMessage); 
+3

你有任何SMTP配置集中在你的配置文件或地方? – decyclone 2010-12-17 09:06:51

+0

解决了problem-我自己的配置问题,导致此和异常的更多的东西一般 – Elena 2010-12-17 09:43:11

回答

3

此代码可能会帮助!

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 
7

为了送你需要一个SMTP服务器的邮件,所以确保你已经在配置文件中指定了一个SMTP服务器。

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="mail.mydomain.com" password="secret" port="25" userName="[email protected]" /> 
     </smtp> 
    </mailSettings> 
</system.net> 
+0

得到了某种程度上也肿了,如果你想asp.net 4之前,使用在任何一个Gmail服务器,那么你将有问题,因为标签只以上刚刚从这个版本开始支持enablessl。 – rtpHarry 2010-12-17 09:16:48