How do I Pass an array to a method

Posted on 2004-09-03
Medium Priority
Last Modified: 2010-04-17
How do I pass the array(in this case it is only one, in the future it will be more
public class TestSchedular_from_main {
    String setRay[]     = {"address@blah.com"};
      public static void main(String[] args) {
      DocChangeChecker dockChg = new DocChangeChecker();
      String recipients[] =  setRay; // how do I do this?
      String subject      = "Testing Batch Email frm a bat file";
      String message      = "Never fear Theodore is here";
      String from         = "address@blah.com";
    PostEmail( recipients, subject, message , from);
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class PostEmail {
      public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     //Virtual IP address(VIP)
     props.put("mail.smtp.host", "999.999.9.999");//Virtual IP address(VIP)
    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
        addressTo[i] = new InternetAddress(recipients[i]);
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Optional : You can also set your custom headers in the Email if you Want
    //msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setContent(message, "text/plain");



Question by:coolgem
  • 2
LVL 32

Expert Comment

ID: 11976495
you have to define static for using a variable in another function...

public class TestSchedular_from_main {

     static String[] setRay = {"address1@blah.com","address2@blah.com"};
     public static void main(String[] args) {
         DocChangeChecker dockChg = new DocChangeChecker();
         String[] recipients = setRay;
         String subject = "Testing Batch Email frm a bat file";
         String message = "Never fear Theodore is here";
         String from = "address@blah.com";
         PostEmail( recipients, subject, message , from);
LVL 32

Accepted Solution

ldbkutty earned 500 total points
ID: 11976525
This works for me:

public class test {

     static String[] setRay = {"test1@email1.com", "test2@email2.com"};

     public static void main(String[] args) {
          String[] recipients = setRay;
      public static void testMethod(String[] rec) {
          for (int i = 0; i<rec.length; i++)
      System.out.println("REC" + rec[i]);

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Starting up a Project

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question