Announcement Announcement Module
Collapse
No announcement yet.
MimeMessage as a Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #2
    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?
    Last edited by Dave Syer; Aug 23rd, 2006, 03:25 AM.

    Comment


    • #3
      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 "to" 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.

      Comment


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

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment


              • #8
                sorry, but neither I have a how-to, nor an example. But I have something in mind, that can help you

                you can define a bean of the Map type, which holds all default properties for your MimeMessage. Then in your code, where you set the "to" address, put the following:

                Code:
                MimeMessage realMessage = .....;
                Map messageProps = (Map)ctx.getBean( "messageProps" );
                ( new BeanWrapperImpl( realMessage ) ).setPropertyValues( messageProps );
                realMessage.setTo( "[email protected]" );
                HTH

                Comment


                • #9
                  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 "..." are filled in from properties of the MimeMessagePreparator bean.

                  Comment


                  • #10
                    Sample Code

                    Hey bjornn,

                    Here is some code to help get you started...

                    Code:
                    package org.spring.forum.mail;
                    
                    import javax.mail.BodyPart;
                    import javax.mail.Message;
                    import javax.mail.internet.InternetAddress;
                    import javax.mail.internet.MimeBodyPart;
                    import javax.mail.internet.MimeMessage;
                    import javax.mail.internet.MimeMultipart;
                    
                    import org.springframework.mail.javamail.MimeMessagePreparator;
                    
                    public class MimeMessagePreparatorImpl implements MimeMessagePreparator {
                    
                        private String from;
                        private String to;
                        private String subject;
                        private String plainTextContent;
                        private String htmlContent;
                    
                        public String getFrom() {
                            return this.from;
                        }
                    
                        public void setFrom(String from) {
                            this.from = from;
                        }
                        
                        public String getTo() {
                            return this.to;
                        }
                    
                        public void setTo(String to) {
                            this.to = to;
                        }
                    
                        public String getSubject() {
                            return this.subject;
                        }
                    
                        public void setSubject(String subject) {
                            this.subject = subject;
                        }
                    
                        public String getPlainTextContent() {
                            return this.plainTextContent;
                        }
                    
                        public void setPlainTextContent(String plainTextContent) {
                            this.plainTextContent = plainTextContent;
                        }
                        
                        public String getHtmlContent() {
                            return this.htmlContent;
                        }
                    
                        public void setHtmlContent(String htmlContent) {
                            this.htmlContent = htmlContent;
                        }
                    
                        public void prepare(MimeMessage message) throws Exception {
                    
                            message.addFrom(InternetAddress.parse(getFrom()));
                            message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                            message.setSubject(getSubject());
                    
                            MimeMultipart mimeMultipart = new MimeMultipart("alternative");
                            message.setContent(mimeMultipart);
                            
                            BodyPart htmlBodyPart = new MimeBodyPart();
                            htmlBodyPart.setContent(getHtmlContent(), "text/html");
                            mimeMultipart.addBodyPart(htmlBodyPart);
                        }
                    
                    }
                    Code:
                    package org.spring.forum.service;
                    
                    public interface RegisterUserService {
                    
                        public void registerNewUser(String username);
                        
                    }
                    Code:
                    package org.spring.forum.service;
                    
                    import org.springframework.mail.javamail.JavaMailSender;
                    import org.springframework.mail.javamail.MimeMessagePreparator;
                    
                    public class RegisterUserServiceImpl implements RegisterUserService {
                    
                        private JavaMailSender javaMailSender;
                        private MimeMessagePreparator successfulMimeMessagePreparator;
                        private MimeMessagePreparator failureMimeMessagePreparator;
                        
                        public void setJavaMailSender(JavaMailSender javaMailSender) {
                            this.javaMailSender = javaMailSender;
                        }
                        
                        public void setSuccessfulMimeMessagePreparator(MimeMessagePreparator successfulMimeMessagePreparator) {
                            this.successfulMimeMessagePreparator = successfulMimeMessagePreparator;
                        }
                        
                        public void setFailureMimeMessagePreparator(MimeMessagePreparator failureMimeMessagePreparator) {
                            this.failureMimeMessagePreparator = failureMimeMessagePreparator;
                        }
                    
                        public void registerNewUser(String userEmailAddress) {
                            
                            //  try {
                            //      addUserMethod(username);
                                    sendSuccessEmail();
                            //  } catch (Exception e) {
                                    sendFailureEmail(); 
                            //  }
                            
                        }
                        
                        public void sendSuccessEmail() {
                            this.javaMailSender.send(this.successfulMimeMessagePreparator);
                        }
                        
                        public void sendFailureEmail() {
                            this.javaMailSender.send(this.failureMimeMessagePreparator);
                        }
                    
                    }
                    Code:
                    <?xml version="1.0" encoding="UTF-8"?>
                    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                    <beans>
                    
                        <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
                            <property name="host">
                                <value>localhost</value>
                            </property>
                        </bean>    
                        
                        <bean id="successfulMimeMessagePreparator" class="org.spring.forum.mail.MimeMessagePreparatorImpl">
                            <property name="from">
                                <value>[email protected]</value>
                            </property>
                            <property name="to">
                                <value>[email protected]</value>
                            </property>
                            <property name="subject">
                                <value>Successful registration!</value>
                            </property>
                            <property name="plainTextContent">
                                <value>You were registered successfully!</value>
                            </property>
                            <property name="htmlContent">
                                <value>&lt;html>&lt;head>&lt;/head>&lt;body>&lt;h1>You were registered successfully!&lt;/h1>&lt;/body>&lt;/html></value>
                            </property>
                        </bean>
                        
                        <bean id="failureMimeMessagePreparator" class="org.spring.forum.mail.MimeMessagePreparatorImpl">
                            <property name="from">
                                <value>[email protected]</value>
                            </property>
                            <property name="to">
                                <value>[email protected]</value>
                            </property>
                            <property name="subject">
                                <value>Unsuccessful registration!</value>
                            </property>
                            <property name="plainTextContent">
                                <value>You were NOT registered successfully!</value>
                            </property>
                            <property name="htmlContent">
                                <value>&lt;html>&lt;head>&lt;/head>&lt;body>&lt;h1>You were NOT registered successfully!&lt;/h1>&lt;/body>&lt;/html></value>
                            </property>
                        </bean>
                    
                        <bean id="registerUserService" class="org.spring.forum.service.RegisterUserServiceImpl">
                            <property name="javaMailSender" ref="javaMailSender"/>
                            <property name="successfulMimeMessagePreparator" ref="successfulMimeMessagePreparator"/>
                            <property name="failureMimeMessagePreparator" ref="failureMimeMessagePreparator"/>
                        </bean>  
                    
                    </beans>
                    I didn't test it, but hopefully that gives you a start. Let me know if you have any questions with it...

                    -Arthur Loder

                    Comment

                    Working...
                    X