2014-09-06 21 views
0

此代碼發送電子郵件到保存在Access數據庫中多電子郵件地址,但我不得不排隊(email =read_Email.GetValue(i).ToString();)問題不能隱式轉換類型「字符串」到「廉政」不能類型「字符串」隱式轉換爲「INT」出於某種原因

任何幫助。

try 
{ 
    ArrayList list_emails = new ArrayList(); 
    int i = 0, email = 0; 
    connection.Open(); //connection to the database. 
    OleDbCommand cmd_Email = new OleDbCommand("Select Email from Email_Table", connection); 
    OleDbDataReader read_Email = cmd_Email.ExecuteReader(); 
    while (read_Email.Read()) 
    { 
     email =read_Email.GetValue(i).ToString(); 
     list_emails.Add(email); //Add email to a arraylist 
     i = i + 1 - 1; //increment or ++i 
    } 
    read_Email.Close(); 
    connection.Close(); //Close connection 

    foreach (string email_to in list_emails) 
    { 
     MailMessage mail = new MailMessage(); 
     mail.To.Add(email_to); 
     mail.Subject = label2.Text + " station " + label1.Text; 
     mail.From = new MailAddress("[email protected]"); 
     mail.Body = "Test"; 
     SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
     smtp.Send(mail); 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Error: " + ex.Message); 
} 
+2

您已經聲明'email'作爲'int'。你分配一個'string'它。該錯誤是告訴你到底是什麼問題。 – 2014-09-06 01:25:00

+0

謝謝你沒有錯誤消息字符串電子郵件 – 2014-09-06 01:32:20

回答

5

你的電子郵件初始化爲整數:

int i = 0, email = 0; 

,你想分配一個字符串值,以它:

email =read_Email.GetValue(i).ToString(); 

你需要要麼使email字符串或分配一個整數值給它。

+0

後謝謝串電子郵件後沒有錯誤信息 – 2014-09-06 01:32:38

0

不需要在循環中創建對象,而是使用類似的東西:

namespace ConsoleApplication3 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Data.OleDb; 
    using System.Linq; 
    using System.Net.Mail; 

    public class Program 
    { 
     public static void Main() 
     { 
      try 
      { 
       List<string> emails = new List<string>(); 

       int i = 0, email = 0; 
       connection.Open(); //connection to the database. 
       OleDbCommand cmd_Email = new OleDbCommand("Select Email from Email_Table", connection); 
       OleDbDataReader read_Email = cmd_Email.ExecuteReader(); 
       if (read_Email.HasRows) 
     { 
       while (read_Email.Read()) 
       { 
        email = read_Email.GetString(0).FirstOrDefault(); 
        emails.Add(email); 
       } 
       read_Email.Close(); 
       connection.Close(); //Close connection 
       MailMessage message = new MailMessage() { 
        Subject = label2.Text + " station " + label1.Text, 
        From = new MailAddress("[email protected]"), 
        Body = "Test"; 
      }; 

      SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
       emails.ForEach(to => 
       { 
        message.To.Clear(); 
        message.To.Add(to); 
        smtp.Send(message); 
       }); 
      } 
} 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error: " + ex.Message); 
      } 
     } 
    } 
} 
相關問題