Method - Send Email to Multiple Users

I'm trying to send a email to multiple users, but get the error " CS0103, The name 'to_email_users' does not exist in the current context" Thanks in advance. Innovator inn=this.getInnovator(); // *** Express ECO ********************************************************* // Item hd= inn.newItem("Express ECO","get"); hd.setID(this.getID()); hd.setAttribute("select","item_number,title,eco_type,team_id"); hd = hd.apply(); // *** Email From ********************************************************** // Item innAdm= inn.newItem("User","get"); innAdm.setAttribute("select","first_name,last_name,email"); innAdm.setProperty("login_name","admin"); innAdm=innAdm.apply(); // *** Email To ************************************************************ // Item toUser1= inn.newItem("team identity","get"); toUser1.setAttribute("select","related_id"); toUser1.setProperty("source_id",hd.getProperty("team_id")); toUser1.setProperty("team_role","6D22DAAF10654038B177A60785E4AA8D"); toUser1=toUser1.apply(); Item toUser= inn.newItem("User","get"); toUser.setAttribute("select","email"); toUser.setProperty("owned_by_id",toUser1.getProperty("related_id")); toUser=toUser.apply();   // *** Email Information *************************************************** // string from_user=innAdm.getProperty("email"); string from_userN = innAdm.getProperty("first_name")+ " " + innAdm.getProperty("last_name"); for (int i = 0; i < toUser.getItemCount(); i++) { string to_email_users = toUser.getItemByIndex(i).getProperty("email"); } string emBody= "<body>You have been identified as one of the members of the Configuration Management Team for this ECP." + "<br>" ; System.Net.Mail.MailAddress SendFrom = new System.Net.Mail.MailAddress(from_user,from_userN); System.Net.Mail.MailAddress SendTo = new System.Net.Mail.MailAddress(to_email_users); System.Net.Mail.MailMessage MyMessage = new System.Net.Mail.MailMessage(SendFrom, SendTo); MyMessage.Subject = "ECP Number: " + hd.getProperty("item_number"); MyMessage.IsBodyHtml = true; MyMessage.Body = emBody;  
  • Hello, The issue you are running in to is that the to_email_users variable is defined only within the context of your for loop. It is not accessible outside of the for loop which is causing the error that you are seeing. One solution would be to move the definition of the to_email_users variable to just before the loop, but this would cause another issue which is that you are overwriting the variable each time you run through the loop. This means that only the last user in the list would actually be sent the email. I have updated the sample that you provided to add the recipients one-by-one directly to the MailMessage. However, please be aware that this sample is untested as I don't have the full context of this Method. While this code compiled on my end, there may be some issue that I was unable to find through testing. Chris _____________________________ Christopher Gillis Aras Labs Software Engineer
  • Hey Chris,
    If it sends to only one "email to" it works fine, but if there's more than one I get the following error:
    "Not a single item".
    THANKS