JavaMail MimeMessage - comma delimited?

  • Thread starter Edward A Thompson
  • Start date
E

Edward A Thompson

I have a small app that builds an ArrayList of Strings (lines of a
report terminated with "\n").

The toString function turns the ArrayList into a single String, with
each 'line' terminated with a "\n".

If I print the resulting String, all is well:

3M DRG Assurance
Validity and Status Report

But when I try to email that String, I get:

[
, 3M DRG Assurance
, Validity and Status Report]

note the brackets and commas. Here is the relevant code:

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to)
);
message.setSubject(subject);
...

message.setText(text.toString);

Can anyone shed some light on this?
 
G

GaryM

(e-mail address removed) (Edward A Thompson) wrote in
Can anyone shed some light on this?

According to RFC822 [ .. ] denote a domain literal, i.e. me@
[127.0.0.1]. In your case it seems Javamail cannot parse the string you
are providing so it is treating it all as a domain litereal. In any
case you have not separated the addresses with commas.

You can try your code by first turning your ArrayList into a comma-
delimited string. Alternatively and slightly easier IMHO is to use
MimeMessage.addRecipients(..) method and loop through each entry in the
list.
 
R

Roedy Green

note the brackets and commas. Here is the relevant code:

First lets see a tiny program that does your toString without throwing
in the complication of MimeMessage. Make sure that part is working.

The whole point of MIME encoding is to send messages though the mail
using only 7-bit bytes. It "quotes" various cantankerous characters,
and reconstitutes them at the far end.

See http://mindprod.com/jgloss/mime.html
 
C

Chris Smith

Edward said:
The toString function turns the ArrayList into a single String, with
each 'line' terminated with a "\n".

If I print the resulting String, all is well:

3M DRG Assurance
Validity and Status Report

But when I try to email that String, I get:

[
, 3M DRG Assurance
, Validity and Status Report]

ArrayList.toString will always give you a comma-separated list in
brackets. I can't explain why you're getting the wrong result from the
first test, but that seems to be the root of your confusion. If you
want to concatenate the String representations of members of an
ArrayList, you'll need to write code to do that.

In general, there are very few situations where it's a good idea to rely
on the result of toString except for debugging. The only one that comes
to mind is StringWriter, which is poorly designed to use toString for
something other than its intended purpose.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Edward A Thompson

GaryM said:
(e-mail address removed) (Edward A Thompson) wrote in
Can anyone shed some light on this?

According to RFC822 [ .. ] denote a domain literal, i.e. me@
[127.0.0.1]. In your case it seems Javamail cannot parse the string you
are providing so it is treating it all as a domain litereal. In any
case you have not separated the addresses with commas.

You can try your code by first turning your ArrayList into a comma-
delimited string. Alternatively and slightly easier IMHO is to use
MimeMessage.addRecipients(..) method and loop through each entry in the
list.

Thanx, but its not the recipient line giving me the problem, its the
text of the message, which is what make sit weird:

message.setText(text.toString);
 
E

Edward A Thompson

First lets see a tiny program that does your toString without throwing
in the complication of MimeMessage. Make sure that part is working.
I did, and its prints to a screen fine.

In the class itself:
....
public String toString() {
StringBuffer sb = new StringBuffer(1024);

ListIterator i = report.listIterator();
while(i.hasNext()) {
sb.append(i.next());
}
return sb.toString();
}
....
where valReport is an instance of the class
....
public static void printReport() {
if(valReport != null)
{
System.out.println(valReport);
//System.out.println(valReport.toString());
}
}
 
E

Edward A Thompson

Chris Smith said:
Edward said:
The toString function turns the ArrayList into a single String, with
each 'line' terminated with a "\n".

If I print the resulting String, all is well:

3M DRG Assurance
Validity and Status Report

But when I try to email that String, I get:

[
, 3M DRG Assurance
, Validity and Status Report]

ArrayList.toString will always give you a comma-separated list in
brackets. I can't explain why you're getting the wrong result from the
first test, but that seems to be the root of your confusion. If you
want to concatenate the String representations of members of an
ArrayList, you'll need to write code to do that.

In general, there are very few situations where it's a good idea to rely
on the result of toString except for debugging. The only one that comes
to mind is StringWriter, which is poorly designed to use toString for
something other than its intended purpose.


I am not using the generic Arraylist.toString though. I am using:

public String toString() {
StringBuffer sb = new StringBuffer(1024);

ListIterator i = report.listIterator();
while(i.hasNext()) {
sb.append(i.next());
}
return sb.toString();
}
 
C

Chris Smith

Edward said:
I am not using the generic Arraylist.toString though. I am using:

public String toString() {
StringBuffer sb = new StringBuffer(1024);

ListIterator i = report.listIterator();
while(i.hasNext()) {
sb.append(i.next());
}
return sb.toString();
}

Okay, it would have helped if you'd mentioned this before. I can now
say that you haven't given enough information to solve your problem.
Even elsewhere in the thread, where you were asked to provide an example
of code that fails in this way, you only provided a couple of snippets
out of context, without being very clear about where the variables come
from. Since the code you're posting doesn't compile (some method
invocations are missing parentheses), I'm skeptical about that being
your real code anyway

It's almost certain that the bug is that you *think* you're using your
own toString method, when you're really using ArrayList's toString...
but without more information I can't begin to suggest how that's
happening.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Edward A Thompson

Since the code you're posting doesn't compile (some method
invocations are missing parentheses), I'm skeptical about that being
your real code anyway

Wasn't trying to be cryptic, was just sending what I thought it be
relevant so others wouldn't have to weed through my code...

It's almost certain that the bug is that you *think* you're using your
own toString method, when you're really using ArrayList's toString...
but without more information I can't begin to suggest how that's
happening.

Bingo! With this info and the behavior of toString for ArrayList that
you sent, I discovered this was exactly what was happening. I was
executing the toString of the ArrayList object instead of the toString
of the object CONTAINING the ArrayList. Thanx for the help!
 
G

GaryM

(e-mail address removed) (Edward A Thompson) wrote in
Thanx, but its not the recipient line giving me the problem, its the
text of the message, which is what make sit weird:

message.setText(text.toString);

Whoops. Sorry. Don't know what track I was on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top