Hi,
I had troubles sending emails with attachement.
I was using a remote SMTP server (from my internet provider) but there will be the same problems with a local SMTP server.
From what I have gathered there are two flaws in MailEngine method to send attachments
( sendMessage(String[] emailAddresses, ClassPathResource resource, String bodyText, String subject, String attachmentName) )
- No from:
There is no "helper.setFrom" call, so the from field is defined with sytem variables (
login@machine_name) and not with the mail.default.from (from the mail.properties file): I suppose THAT'S A BUG.
Then the SMTP server from my internet provider refuses it because it's not a valid internet email address (on windows machine name are juste machine name not in the internet form) ... otherwise that would not be too important (I assume a local SMTP server would not care).
- ClassPathResource attached file
The file is read using ClassPathResource, but the file I want to send by email is not in the Class Path and I would expect that would be the case for quite a few developments.
So I created a new sendMessage method defining a from address (I think it's still necessary to have, even if mail.default.from would work) and an attachment as a File (see below) !
Whether it is interesting enough to integrate into the latest AppFuse, it's up to you to decide.
Cheers,
L@u
The Computing Froggy
===================== sendMessage =========
/**
* Convenience method for sending messages with attachments with a from source and File
*
* @param emailAddresses
* @param attachedFile
* @param bodyText
* @param subject
* @param attachmentName
* @throws MessagingException
* @author Ben Gill
*/
public void sendMessage(String fromAdress, String[] emailAddresses,
File attachedFile, String bodyText,
String subject, String attachmentName)
throws MessagingException {
MimeMessage message =
((JavaMailSenderImpl) mailSender).createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(emailAddresses);
helper.setText(bodyText);
helper.setSubject(subject);
helper.setFrom(fromAdress);
helper.addAttachment(attachmentName, attachedFile);
((JavaMailSenderImpl) mailSender).send(message);
}
================================