Back Forum Reply New

MimeMessage as a Bean

Hi all,

I was just wondenring if it's possible to create a MimeMessage as a bean in my applicationContext.xml. I have read mail tutorial but I just like to use MimeMessage instead of SimpleMessage.

I would like to define a message with subject, text, from, etc.. and get it as a bean. This message would be triggered every time an user create a account in my application... so, the text, subject, content type and other parameters are all the same.

I didn't found any doc or tutorial about it. Is it possible? If yes, how?

Thanks a lot.
Best regards.

Did you mean MimeMailMessage?  I'm not sure it's supposed to work that way.  You might be able to write a message preparator that can turn your SimpleMailMessage into a mime message in a callback on the JavaMailSender.  But actually, that is what the JavaMailSenderImpl does already, so what would you gain from having a MimeMailMessage as a bean in the application context?

Hi David,

Yes, I want to say MimeMailMessage.

I want to put it as a bean because I don't want to set the text, subject, from, and so on..., every time I need to send a message.

I was thinking that I could create a bean with a name like SubscriptionMessage, and every time a new account is created I just pick up the bean, change the quot;toquot; attribute, and send...

Got it?

But answer me another thing: I'm trying to use MimeMessage because I want to send html messages. Am I right? For html messages I must use MimeMessage? Or it's a misconcept?

Thanks again.

simply define it in your ctx as a prototype (or a prototypetargetsource), so that each time you are getting it, it's created new...

Hi injecteer,

I have tried to declare a MimeMailMessage bean in my applicationContext.xml, but I'm having trouble to declare the bean. A MimeMessage needs a Session. A session needs x... and x needs y and at last, I have tried to create a Session and pass some properties as constructor args... but a session needs a PasswordAuthenticator too.... A session needs a folder... I got very confused...

Can you show me some sample, tutorial or article how to declare it?

Thanks.

To send an html-message you don't have to play with MimeMessage. If your message contains only html-text w/o attachements, you can go for SimpleMailMessage which can be easily defined as a bean.

Great...

But I think that if I could set a MimeMessage as as bean it would be best...
Because maybe the images of html could be send as a part of the message, so the user do need to access(involuntary) my website to see the images...

Most corporation proxys or mail application prevent this action...

Please... a tutorial or sample of how to set a MimeMessage as bean.

Thanks anyway.

quot; );
HTH

I think the point of MimeMessage is that you have to have a session to create one, so they are only created at the time they are sent.  Spring provides the MimeMessagePreparator interface to address this issue.  You can implement one of these, and set all the properties you like in the ApplicationContext.  Then when it comes to send a message pass it to the JavaMailSender instead of an actual message.  The key is the implementation of your MimeMessagePreparator.prepare method, e.g.Code:
void prepare(MimeMessage mimeMessage) throws Exception {   mimeMessage.setFrom(...);   mimeMessage.setTo(...);   // etc...
}
where the quot;...quot; are filled in from properties of the MimeMessagePreparator bean.

lt;/valuegt;       lt;/propertygt;       lt;property name=quot;toquot;gt;lt;valuegt;newUser@gmail.comlt;/valuegt;       lt;/propertygt;       lt;property name=quot;subjectquot;gt;lt;valuegt;Successful registration!lt;/valuegt;       lt;/propertygt;       lt;property name=quot;plainTextContentquot;gt;lt;valuegt;You were registered successfully!lt;/valuegt;       lt;/propertygt;       lt;property name=quot;htmlContentquot;gt;lt;valuegt;amp;lt;htmlgt;amp;lt;headgt;amp;lt;/headgt;amp;lt;bodygt;amp;lt;h1gt;You were registered successfully!amp;lt;/h1gt;amp;lt;/bodygt;amp;lt;/htmlgt;lt;/valuegt;       lt;/propertygt;   lt;/beangt;      lt;bean id=quot;failureMimeMessagePreparatorquot; class=quot;org.spring.forum.mail.MimeMessagePreparatorImplquot;gt;       lt;property name=quot;fromquot;gt;lt;valuegt;someEmailAddress@gmail.comlt;/valuegt;       lt;/propertygt;       lt;property name=quot;toquot;gt;lt;valuegt;newUser@gmail.comlt;/valuegt;       lt;/propertygt;       lt;property name=quot;subjectquot;gt;lt;valuegt;Unsuccessful registration!lt;/valuegt;       lt;/propertygt;       lt;property name=quot;plainTextContentquot;gt;lt;valuegt;You were NOT registered successfully!lt;/valuegt;       lt;/propertygt;       lt;property name=quot;htmlContentquot;gt;lt;valuegt;amp;lt;htmlgt;amp;lt;headgt;amp;lt;/headgt;amp;lt;bodygt;amp;lt;h1gt;You were NOT registered successfully!amp;lt;/h1gt;amp;lt;/bodygt;amp;lt;/htmlgt;lt;/valuegt;       lt;/propertygt;   lt;/beangt;
   lt;bean id=quot;registerUserServicequot; class=quot;org.spring.forum.service.RegisterUserServiceImplquot;gt;       lt;property name=quot;javaMailSenderquot; ref=quot;javaMailSenderquot;/gt;       lt;property name=quot;successfulMimeMessagePreparatorquot; ref=quot;successfulMimeMessagePreparatorquot;/gt;       lt;property name=quot;failureMimeMessagePreparatorquot; ref=quot;failureMimeMessagePreparatorquot;/gt;   lt;/beangt;  

lt;/beansgt;
I didn't test it, but hopefully that gives you a start. Let me know if you have any questions with it...

-Arthur Loder
¥
Back Forum Reply New