Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

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
Avatar of Mayank S
Mayank S
Flag of India image

>> 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.
>> // 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.
Avatar of princehyderabad
princehyderabad

ASKER

>>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.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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 () ;
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
Looks ok.