JavaMail - RFC822

R

Rico

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Pattern p = Pattern.compile("@[a-zA-Z_0-9<>\\-\\;\\,\\.]*@");
Matcher m = p.matcher(user_info.getEmailAddress());
boolean b = m.find();

if (!b) // Does _not_ match 2 or more email addresses
{
InternetAddress[] addr = {new InternetAddress(user_info
.getEmailAddress(), user_info.getName())};
msg.addRecipients(Message.RecipientType.TO, addr);
} else
msg.addRecipients(Message.RecipientType.TO, user_info
.getEmailAddress());
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In the above code, I use the regular expression to detect whether I
have at least 2 email addresses to send the message to.
This is because I can't figure out how to attach a 'name' string
to a group of email addresses in JavaMail. So in the first case when
the match fails (only 1 email address) I can afford to put the name
string, otherwise, I don't.

Previously a hyphen in the domain name caused the regular expression
to fail to detect 2 email addresses because I didn't include it as a
possible literal.

Now, if I use (e-mail address removed);[email protected] , wanting to be RFC822 compliant,
Javamail complains that the semi-colon is illegal: not in group.
Trivial fix is to replace the semi-colon with comma.

Yet, not that I lack other things to do, I did go and read RFC822 to
find out how to associate a name with a list of email addresses and
they say:

group = phrase ":" [#mailbox] ";"

The part between square brackets is my list of email addresses, separated
by commas.

So, that'd mean I use:
"marc and tommy":[email protected],[email protected];

Not surprisingly, it doesn't work.

Maybe someone could please help me make sense of what I've read then.
How do I associate a name string with a group of email addresses?
And where does JavaMail expect the semi-colon to be for a group? Thanks.

Rico.
 
B

Barry Margolin

Rico said:
Now, if I use (e-mail address removed);[email protected] , wanting to be RFC822 compliant,
Javamail complains that the semi-colon is illegal: not in group.
Trivial fix is to replace the semi-colon with comma.

Yet, not that I lack other things to do, I did go and read RFC822 to
find out how to associate a name with a list of email addresses and
they say:

group = phrase ":" [#mailbox] ";"

The part between square brackets is my list of email addresses, separated
by commas.

So, that'd mean I use:
"marc and tommy":[email protected],[email protected];

Not surprisingly, it doesn't work.

Maybe someone could please help me make sense of what I've read then.
How do I associate a name string with a group of email addresses?
And where does JavaMail expect the semi-colon to be for a group? Thanks.

Maybe the problem isn't with JavaMail, but with your SMTP server. Group
syntax is somewhat arcane, and I wouldn't be surprised if there are some
servers that don't recognize it.
 
G

GaryM

Maybe someone could please help me make sense of what I've read
then. How do I associate a name string with a group of email
addresses? And where does JavaMail expect the semi-colon to be for
a group? Thanks.


Rico, I tested this out and it works fine. Perhaps you can post your
code and any exceptions along with debug log? Here's my test code.

// My test code:

String group = "\"marc and tommy\":[email protected],[email protected];";

// Does Javamail like the group construction?
try {
InternetAddress addr = new InternetAddress(group);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}

System.setProperty("mail.debug", "true");
System.setProperty("mail.smtp.auth", "true");
System.setProperty("mail.transport.protocol", "smtp");

Session session = Session.getDefaultInstance(System.getProperties());
MimeMessage mm = new MimeMessage(session);

// Create and send the message
try {
mm.setFrom(new InternetAddress("(e-mail address removed)"));
mm.setRecipients(Message.RecipientType.TO, group);
mm.setSubject("Test groups");
mm.setText("This is a test");
mm.saveChanges();
Transport transport= session.getTransport();
transport.connect("your_server");
transport.sendMessage(mm, mm.getAllRecipients());
} catch (MessagingException e1) {
e1.printStackTrace();
}
 
R

Rico

Rico, I tested this out and it works fine. Perhaps you can post your
code and any exceptions along with debug log? Here's my test code.

Thanks a lot. Things work fine with Sun's JavaMail. I ran into a couple of
glitches with GNU's version.
In both cases it's better to not create an InternetAddress or
InternetAddress[] from the group because MimeMessage.getAllRecipients()
fails. Using the string itself is all good for Sun; attachment included.

With GNU's JavaMail I run into some GSSException and some missing krb5.ini
file even though the email does get sent out but to only the first address
in the group and to the CC field. Furthermore, the PDF attachment is seen
as a text document. I've subscribed to the GNU JavaMail mailing list a few
days back. I'll email them about it.
// My test code:
String group = "\"marc and tommy\":[email protected],[email protected];";

// Does Javamail like the group construction?
try {
InternetAddress addr = new InternetAddress(group);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}

System.setProperty("mail.debug", "true");
System.setProperty("mail.smtp.auth", "true");
System.setProperty("mail.transport.protocol", "smtp");

Session session = Session.getDefaultInstance(System.getProperties());
MimeMessage mm = new MimeMessage(session);

// Create and send the message
try {
mm.setFrom(new InternetAddress("(e-mail address removed)"));
mm.setRecipients(Message.RecipientType.TO, group);
mm.setSubject("Test groups");
mm.setText("This is a test");
mm.saveChanges();
Transport transport= session.getTransport();
transport.connect("your_server");
transport.sendMessage(mm, mm.getAllRecipients());
} catch (MessagingException e1) {
e1.printStackTrace();
}

Rico.
 
R

Rico

Maybe the problem isn't with JavaMail, but with your SMTP server. Group
syntax is somewhat arcane, and I wouldn't be surprised if there are some
servers that don't recognize it.

Thanks Barry.
I guess I could have a loop that extracts the individual addresses as
tokens using space, comma and semi-colon as separators and add them into
an InternetAddress[].
I think Sun's JavaMail might save me the trouble though.

Rico.
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top