Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with JavaMail on Tomcat

Status
Not open for further replies.

KGarreis

Programmer
Jul 26, 2005
3
DE
Hello,

I'm currently trying to get the following scenario working:
I have a HTML form fields for text input and a file upload field. On submit the content of the fields should be e-mailed to a certain address with the uploaded file as attachment. I realize this with a Java Servlet and Java class using JavaMail API.
When I'm sending the mail without attachments it works fine.
When I'm using only the class (without the HTML and servlet who is calling it) the mail gets properly send with the attachment.
However, if I'm using the HTML/servlet constellation I don't receive a proper e-mail, only the following in the mail message:
[MIME content for this item is stored in attachment $RFC822.eml. Parsing MIME content failed: Incorrect format in MIME data..]

I assume that it might be something with my Tomcat configuration, because I eliminated with my tests all other possibilities. I'm using Tomcat 5.5.9 on Windows XP (for testing). One other thing also made me think that it might be related to Tomcat: I didn't get the mail/Session on Tomcat running. I followed the instructions on to set up the environment for the mail/Session, but it didn't work, I couldn't send e-mails at all. Unfortunately I also have no error messages in the Tomcat log files. Because of time pressure I worked around this problem, however I think that the two problems might relate.
Has anyone experienced similar problems? Are there any configurations I can set so that the mail content-type is set? Any ideas to the problem with the mail/Session?
Any ideas/suggestions/hints are really appreciated.

Regards, Karin
 
A bit of code might help (only the relevant sections please) ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
OK, here is the code from the Java class, I think this will be the relevant part.
Code:
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", sHost);
props.setProperty("mail.user", sUser);
props.setProperty("mail.password", sPass);
	    
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);

try {
  Transport transport = session.getTransport();
  Message message = new MimeMessage(session);

  Address fromAddress = new InternetAddress(sSender); 
  Address toAddress = new InternetAddress(sRecip);

  message.setSubject(sSubject);
  message.setFrom(fromAddress);
  message.addRecipient(Message.RecipientType.TO,toAddress);

  MimeBodyPart mBP1 = new MimeBodyPart();
  mBP1.setText(sContent);
  MimeBodyPart mBP2 = new MimeBodyPart();
  DataSource fds = new FileDataSource(sFile);
  mBP2.setDataHandler(new DataHandler(fds));
  mBP2.setFileName(sFilename);
        
  MimeMultipart multipart = new MimeMultipart();
  multipart.addBodyPart(mBP1);
  multipart.addBodyPart(mBP2);
  message.setContent(multipart);
  message.saveChanges();

  transport.connect();
  transport.sendMessage(message, message.getAllRecipients ());
  transport.close();
  return true;
}
catch (Exception ex) {
  System.err.println("Cannot send email. " + ex + "\n");
  return false;
}
The HTML form contains for testing at the moment just the submit button, and the action of the form calls the Java Servlet. This is just calling the above method. All the strings(host name, etc.) are at the moment hardcoded in the class, but only for the testing.

Do you think this is enough or do you need other code as well?

Regards, Karin
 
Hmm, not sure what you are doing wrong - I've tested your code, and it seems OK to me - via the standalone class from the command line, or from the servlet. Perhaps you were not passing parameters in correctly ?

For the record, below are the mail helper, servlet and html form used to test :

Code:
<html>
<body>

<form action="/TestMailServlet" method="GET">
	<input type="text" name="sHost" value="mailer.bbbb.co.uk"/><br/>
	<input type="text" name="sSender" value="bbb.bbb@bbbb.co.uk"/><br/>
	<input type="text" name="sRecip" value="bbb.bbb@bbbb.co.uk"/><br/>
	<input type="text" name="sFilename" value="C:/java/TestMail.java"/><br/>
	<input type="text" name="sContent" value="Some content"/><br/>
	<input type="text" name="sSubject" value="Testing subject"/><br/>
	<input type="Submit"/><br/>
</form>

</body>
</html>

Code:
package test;
import javax.servlet.http.*;
import javax.servlet.*;

public class TestMailServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		String sHost = request.getParameter("sHost");
		String sSender = request.getParameter("sSender");
		String sRecip = request.getParameter("sRecip");
		String sFilename = request.getParameter("sFilename");
		String sContent = request.getParameter("sContent");
		String sSubject = request.getParameter("sSubject");

		new TestMail().doIt(sHost, sSender, sRecip, sFilename, sContent, sSubject);



	}

}

Code:
package test;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.*;

public class TestMail {
	public static void main(String args[]) {
		String sHost = "mailer.bbbb.co.uk";
		String sSender = "bbb.bb@bbb.co.uk";
		String sRecip = "bb.bbb@bbbb.co.uk";
		String sFilename = "C:/java/TestMail.java";
		String sContent = "Hello JavaMail World !";
		String sSubject = "This is a test";

		new TestMail().doIt(sHost, sSender, sRecip, sFilename, sContent, sSubject);

	}

	public boolean doIt(	String sHost,
							String sSender,
							String sRecip,
							String sFilename,
							String sContent,
							String sSubject) {


		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", sHost);
		//props.setProperty("mail.user", sUser);
		//props.setProperty("mail.password", sPass);

		Session session = Session.getDefaultInstance(props, null);
		session.setDebug(true);

		try {
		  Transport transport = session.getTransport();
		  Message message = new MimeMessage(session);

		  Address fromAddress = new InternetAddress(sSender);
		  Address toAddress = new InternetAddress(sRecip);

		  message.setSubject(sSubject);
		  message.setFrom(fromAddress);
		  message.addRecipient(Message.RecipientType.TO,toAddress);

		  MimeBodyPart mBP1 = new MimeBodyPart();
		  mBP1.setText(sContent);
		  MimeBodyPart mBP2 = new MimeBodyPart();
		  DataSource fds = new FileDataSource(sFilename);
		  mBP2.setDataHandler(new DataHandler(fds));
		  mBP2.setFileName(sFilename);

		  MimeMultipart multipart = new MimeMultipart();
		  multipart.addBodyPart(mBP1);
		  multipart.addBodyPart(mBP2);
		  message.setContent(multipart);
		  message.saveChanges();

		  transport.connect();
		  transport.sendMessage(message, message.getAllRecipients ());
		  transport.close();
		  return true;
		}
		catch (Exception ex) {
		  System.err.println("Cannot send email. " + ex + "\n");
		  return false;
		}
	}
}


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj,

thanks very much for your help.
I haven't solved the problem yet, but I have a new starting point.
You are correct, when you say that your test above is working fine, I tried it myself and it's working.
It seems that the problem is not my code, but the surrounding intranet environment where I have to put my code. So I can start now and test parts of the intranet to see if I can generate the error also with the test.
I'll post it when I found the real reason.

Thanks again!
Regards, Karin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top