I am trying to zip a number of HTTP documents, and send these by email as
an attachment (one zip-file).
There are several questions about similar things on this group and
elsewhere, but the problem that I have does not seem to be dealt with. I
keep getting
javax.mail.SendFailedException: Send failure
> (javax.mail.MessagingException: Illegal Arguments
> (java.lang.IllegalArgumentException: Bad Request: Missing body))
>
This only happens on the GAE production server. On my development
installation (GAE Eclipse plugin) everything is fine (except that a
development server does not send email).
The GAE documentation is a bit ambiguous on whether sending zip attachments
is allowed or not. From what I have read there and elsewhere, I understand
that it is okay as long as the zip file contains files with 'allowed'
extensions (like .html). But somehow the "missing body" seems to say that
something is missing. (Yes, I do use setText() on the message.) When I
don't add the attachment, everything works fine, so the missing body must
be the attachment.
If anyone can clarify what is wrong, or even better suggest a fix, that
would be much appreciated. I hope it will also help others facing similar
problems.
The code, brought down to the essentials, but still a bit long, is as
follows:
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(baos);
for (int messageNr = 0; messageNr < 10; ++messageNr) {
String messageText = "<html><body>"+"Message
#"+messageNr+"</body></html>";
String entryName = "message-"+messageNr+".html";
zipOut.putNextEntry(new ZipEntry(entryName));
zipOut.write(messageText.getBytes());
zipOut.closeEntry();
} // for messageNr
zipOut.close();
try {
Properties props = new Properties();
javax.mail.Session mailSession =
javax.mail.Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(gaeAdminEmailAddress, "From me"));
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(gaeAdminEmailAddress, "To you"));
msg.setSubject("messages");
msg.setText("Your messages are in the zipped attachment.");
Multipart mp = new MimeMultipart();
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("messages.zip");
//attachment.setDisposition(Part.ATTACHMENT); // Is this needed,
and does it work?
DataSource zipSource = new ByteArrayDataSource(baos.toByteArray(),
"application/zip");
attachment.setDataHandler(new DataHandler(zipSource));
//attachment.setContent(baos.toByteArray(), "application/zip"); //
Doesn't work, see https://gist.github.com/726458
mp.addBodyPart(attachment);
msg.setContent(mp);
msg.saveChanges(); // Is this needed?
(https://groups.google.com/forum/#!msg/google-appengine-java/5LwT4JG6LiY/bXwf2d_lUKYJ)
Transport.send(msg); // ERROR: javax.mail.SendFailedException: Send
failure (javax.mail.MessagingException: Illegal Arguments
(java.lang.IllegalArgumentException: Bad Request: Missing body))
} catch (AddressException e) {
e.printStackTrace(response.getWriter());
} catch (MessagingException e) {
e.printStackTrace(response.getWriter());
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-appengine-java/-/YN15QQYP3OgJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.