Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Subject line 'space' character is lost when sending mail?

843834Aug 12 2009 — edited Aug 13 2009
Hi,

I'm using JavaMail, and when sending a mail with a subject line that is longer than 62 characters, the last space before the 63rd character becomes a linefeed. This means that when the recipient receives the e-mail, the space in the subject line is actually dropped (becomes a linefeed).

For example. I send an email with the following subject line: "New User Setup [ITNUS-0005] User Setup Progress Report for: Louise Gans".

The recipient will receive the following subject: "New User Setup [ITNUS-0005] User Setup Progress Report for: LouiseGans".

Note the missing space between "Louise Gans". Perhaps someone could test sending out that subject line, and see if they get the same problem?

Looking at the message source on the recipient client, it shows a line feed / new line where that space is supposed to be.

Using MS Outlook or GroupWise or another mail client, the subject lines gets sent through perfectly using the same mail server (Groupwise Internet Agent 7.0.3), but using JavaMail to send the mail through that mail server gives me the space problem.

Can anybody shed some light on why this is happening? I would really appreciate the help.

Here is an example of the code I am testing with:
public class SendHtml {

	public static void main(String[] argv) {
		new SendHtml();
	}

	public SendHtml() {
		String  to = "davidr@mycompany.co.za";
		String subject = "New User Setup [ITNUS-0005] User Setup Progress Report for: Louise Gans";
		String from = "davidr@mycompany.co.za"; 
		String cc = null;
		String bcc = null;
		String url = null;
		String mailhost = "mail.server.co.za";
		String mailer = "sendhtml";
		String protocol = null, host = null, user = null, password = null;
		String record = null;	// name of folder in which to record mail
		boolean debug = false;
		BufferedReader in =
			new BufferedReader(new InputStreamReader(System.in));
		try {
			Properties props = System.getProperties();
			if (mailhost != null) {
				props.put("mail.smtp.host", mailhost);
			}
			// Get a Session object
			Session session = Session.getInstance(props, null);
			// construct the message
			Message msg = new MimeMessage(session);
			if (from != null) {
				msg.setFrom(new InternetAddress(from));
			}
			else {
				msg.setFrom();
			}
			msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to, false));
			if (cc != null)
				msg.setRecipients(Message.RecipientType.CC,
						InternetAddress.parse(cc, false));
			if (bcc != null)
				msg.setRecipients(Message.RecipientType.BCC,
						InternetAddress.parse(bcc, false));
			msg.setSubject(subject);
			collect(in, msg);
			msg.setHeader("X-Mailer", mailer);
			msg.setSentDate(new Date());
			// send the thing off
			Transport.send(msg);
			System.out.println("\nMail was sent successfully.");

			// Keep a copy, if requested.

			if (record != null) {
				// Get a Store object
				Store store = null;
				if (url != null) {
					URLName urln = new URLName(url);
					store = session.getStore(urln);
					store.connect();
				} else {
					if (protocol != null)		
						store = session.getStore(protocol);
					else
						store = session.getStore();

					// Connect
					if (host != null || user != null || password != null)
						store.connect(host, user, password);
					else
						store.connect();
				}

				// Get record Folder.  Create if it does not exist.
				Folder folder = store.getFolder(record);
				if (folder == null) {
					System.err.println("Can't get record folder.");
					System.exit(1);
				}
				if (!folder.exists())
					folder.create(Folder.HOLDS_MESSAGES);

				Message[] msgs = new Message[1];
				msgs[0] = msg;
				folder.appendMessages(msgs);

				System.out.println("Mail was recorded successfully.");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void collect(BufferedReader in, Message msg)
	throws MessagingException, IOException {
		String line;
		String subject = msg.getSubject();
		StringBuffer sb = new StringBuffer();
		sb.append("<HTML>\n");
		sb.append("<HEAD>\n");
		sb.append("<TITLE>\n");
		sb.append(subject + "\n");
		sb.append("</TITLE>\n");
		sb.append("</HEAD>\n");

		sb.append("<BODY>\n");
		sb.append("<H1>" + subject + "</H1>" + "\n");

		sb.append("</BODY>\n");
		sb.append("</HTML>\n");

		msg.setDataHandler(new DataHandler(
				new ByteArrayDataSource(sb.toString(), "text/html")));
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 10 2009
Added on Aug 12 2009
2 comments
1,289 views