2014-09-06 95 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); 
      } 
     } 
    } 
} 
相关问题