2010-12-17 73 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