Solved

How do I Pass an array to a method

Posted on 2004-09-03
2
244 Views
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);
    session.setDebug(debug);

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

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

    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.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}

 

      

}
0
Comment
Question by:coolgem
  • 2
2 Comments
 
LVL 32

Expert Comment

by:ldbkutty
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);
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 125 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;
          testMethod(recipients);
      }
      
      public static void testMethod(String[] rec) {
          for (int i = 0; i<rec.length; i++)
      System.out.println("REC" + rec[i]);
      }
}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now