Appending String and sending

hi experts,

I'm not getting logic to append string and pass it to other class.

Here is thing:
Page.java
...
while (..)
{

//Let say I got this values (134,449,5324, 3434,..)
 ? How can I add above  list of User# in array or whatever is suggestive
FromEmailAddress = //something
ToEmailAddress = //something
}
MailJava mj = new MailJava;
 mj.sendMail(FromEmailAddress, ToEmailAddress, ????How to have those list of user # here????);
..
-----------------------------------

MailJava.java
...
 public String sendMail(java.lang.String  from, java.lang.String to, java.lang.String[] user-no)
 // Is it correct -->java.lang.String[] user-no
...
Message = "Following are Users :"
// how can I display user # each on a line like this:
// 134
// 449
// 5324
//  3434
..
..
Transport.send(message);    


thx,
PH
princehyderabadAsked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
You can append it to the message by using a StringBuffer/ StringBuilder (Java 5) or string-concatenation using the + operator (which is bad).

StringBuffer sb = new StringBuffer ( initialMessage ) ;

if ( conditions satisfied )
  sb.append ( Integer.toString ( whatever number you want to append ) ) ;

message.setText ( sb.toString () ) ;
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Let say I got this values (134,449,5324, 3434,..)

Are these user-IDs to which you have to send mail? (I assume they are not just numbers but valid e-mail IDs.) You need to split them:

String[] userIDs = yourString.split ( "," ) ;

Then loop through these user-IDs and call sendMail ():

for ( int i = 0 ; i < userIDs.length ; i ++ )
  mj.sendMail ( fromAddress, userIDs[i], .... ) ;

And you can make your sendMail () method static, from the looks of it. Just call MailJava.sendMail () then, instead of creating an object of MailJava which has no state.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> // Is it correct -->java.lang.String[] user-no

I assumed above in my comment that its a String, not a String[], so you can call this method inside a loop.

If you want to accept a String[] in the method, the loop should go inside the method.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
princehyderabadAuthor Commented:
>>Are these user-IDs to which you have to send mail? (I assume they are not just numbers but valid e-mail IDs.) You need to split them:

No they are not email ID's. I already took care of email ID list. Bascially those list of numbers I'm getting from DB and need to send email saying which numbers were damaged or whatever. In my Java mail I already set to, from, subject. Just need to list those numbers in body message. How to append those numbers in body message thats what I like to know. And those numbers can be one, few, or many depends on logical condition in code and DB values.

I hope I made it clear.
0
 
princehyderabadAuthor Commented:
Let says I used StringBuffer.

Page.java
..
While (condition)
{
  if ( conditions satisfied )
  sb.append ( Integer.toString ( whatever number you want to append ) ) ;
}
//Calling Mail Method:
  MailJava mj = new MailJava;
 mj.sendMail(From, To, sb);             ---> Is this correct here

------------------------

MailJava.java
...
 public String sendMail(java.lang.String  from, java.lang.String to, java.lang.StringBuffer sb)   --> Is this correct here
{
...
              //Set  the Message Body
                 String textmessage = "The followings are damged:\n"+sb   --> Is this line correct bcoz StringBuffer is assigning to String ??
                 message.setText(textmessage);
  ..
}


FYI: msg should something like this:

The followings are damaged:
111
222
333
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, that should be ok. Make sure you also do an sb.append ( "\r\n" ) after sb.append ( Integer.toString ( whatever number you want to append ) ) ; if you need new-lines in between the numbers.

>> String textmessage = "The followings are damged:\n"+sb  

Should be ok because it will use sb.toString () and concatenate it. Or you could simply accept a String parameter in your method, and pass sb.toString () to it:

mj.sendMail ( From, To, sb.toString () ) ;            

Also, its better to use StringBuffer () for textmessage, like:

String textmessage = new StringBuffer ( "The followings are damged: \r\n" ).append ( sb.toString () ).toString () ;
0
 
princehyderabadAuthor Commented:
Something like this:
..
sb.append ( Integer.toString ( rs.getString("xxxx") ) ) ;
sb.append ( "\r\n" ) ;
...
//Calling Mail Method in other class
mj.sendMail(From, To, sb);

----------------------------------
Mail Class
..
public String sendMail(java.lang.String  from, java.lang.String to, java.lang.StringBuffer sb)
{
..
String textmessage = new StringBuffer ( "The followings are damged: \r\n" ).append ( sb.toString () ).toString () ;
..
}


FYI: I dont hv server acces so cant test live so asking here to make sure. I can work 2morr morning on this to make sure it work.
THX,
PH
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Looks ok.
0
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.

All Courses

From novice to tech pro — start learning today.