Friday, January 21, 2005
MimeMessages sending them with Spring
Alright, so I am charting new territory. I have my shiny new SMTP server up and running and I am currently testing the Spring email functionality. So far this is what I have for a test:
public void send(){
final FileSystemResource res = new FileSystemResource(new File("c:/girls2.jpg"));
if(res == null){
System.out.println("The resource is null!");
}else{
System.out.println(res.getFile().getAbsolutePath());
}
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("chriskwiatkowski@charter.net");
message.setTo(new InternetAddress("chriskwiatkowski@charter.net"));
message.setSubject("my subject");
message.setText("my text <img src='cid:girls2'>", true);
message.addInline("girls2", res );
message.addAttachment("girls2.jpg", res);
}
});
}
I ran this method and so far, not so good. I must be missing something... because when I receive an email it comes out looking like this:
This message contains multimedia content (MIME type=type/unspecified)My initial reaction is that I need to specify a MIME type on this badboy.
that cannot be displayed in your browser. If you have an external player
or plugin that supports this type, you can click here to launch your player.
If you do not have a compatible external player, click the link below.
Multimedia Message: Display parts separately
The following link was helpful:
http://monkeymachine.co.uk/spring/xref/org/springframework/mail/javamail/MimeMessageHelper.html
Well after trying to find a way to set the MIME Type of the file I was attaching I finally went to the API and started looking at the addAttachment method (good idea looking at the API huh?)
public void addAttachment(String attachmentFilename,
File file)
throws MessagingException
- Add an attachment to the MimeMessage, taking the content from a
java.io.File.The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!
- Parameters:
attachmentFilename- the name of the attachment as it will appear in the mailfile- the File resource to take the content from- Throws:
MessagingException- in case of errors- See Also:
addAttachment(String, org.springframework.core.io.InputStreamSource),addAttachment(String, javax.activation.DataSource)
Testing ;)
message.addAttachment("girls2.jpg", new File("c:/girls2.jpg"));
Ok so that didn't work.
Try this:
message.addAttachment("girls2.jpg", new FileSystemResource("c:/girls2.jpg"));
Testing ;)
That didn't work... still the same message when I attempt to view the email.
I am giving up for right now... I will come back to this later.
Ok, I have another observation.
I went ahead and sent the very same test message from my UGA email account to my Charter email account and I noticed that their is a difference in the email headers that may or may not be significant in my issue:
multipart/mixed on my UGA email with attachment
multipart/related on my java based test email from JUNIT
Now I am trying to send emails with attachments without using the MimeMessageHelper class.
I got it working with the following code ripped from the JGURU site:
// Define message
MimeMessage message = mailSender.createMimeMessage();
message.setFrom(new InternetAddress("ckwiat@uga.edu"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("chriskwiatkowski@charter.net"));
message.setSubject("Hello JavaMail Attachment");
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(new File("c:/girls2.jpg"));
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName("girls2.jpg");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
mailSender.send( message );
Now I am really done for a little while... my eyes hurt.
