Announcement Announcement Module
Collapse
No announcement yet.
Issues sending HTML email with JavaMailSenderImpl Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issues sending HTML email with JavaMailSenderImpl

    So I have been trying to figure out how to send HTML email.

    This is my bean:

    Code:
            <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host"><value>some.mail.server</value></property>
    		<property name="port"><value>25</value></property>
    	</bean>
    And in my service I tried using this:

    Code:
                    javaMailSender = new JavaMailSenderImpl();
                    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                    try {
                        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                        helper.setFrom(systemEmailAddress);
                        helper.setTo(toUser.getProfile().getEmail());
                        helper.setSubject(messageTitle);
                        helper.setText(messageBody, true);
                    } catch (Exception exc) {
                        logger.info("some message");
                    }
    
                    try {
                        javaMailSender.send(mimeMessage);
                    } catch (MailException ex) {
                        System.err.println(ex.getMessage());
                    }
    I get this exception:
    Code:
    SEVERE: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
      nested exception is:
            java.net.ConnectException: Connection refused: connect
    So I decided to use a different method to send an email and I found this code that works perfectly:

    Code:
    try {
            String host = "some.mail.server";
            // Get system properties
            Properties props = System.getProperties();
    
            // Setup mail server
            props.put("mail.smtp.host", host);
    
            // Get session
            Session session = Session.getDefaultInstance(props, null);
    
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setRecipient(RecipientType.TO, new InternetAddress(toUser.getProfile().getEmail()));
            message.setFrom(new InternetAddress(systemEmailAddress));
            message.setSubject(messageTitle);
            message.setContent(messageBody, "text/html");
    
            // Send message
            Transport.send(message);
    
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    What I noticed about this codes is this line (first parameter specifies the host to send email to):

    Code:
    // Setup mail server
    props.put("mail.smtp.host", host);
    If I take that host to send the email to away I get a similar exception to the one thrown by the JavaMailSender method (Could not connect to SMTP host: localhost, port: 25).

    So my question is HOW can I tell the bean what host to send the email to? I didn't find any property that would work for it....

    Thanks!

  • #2
    can anyone chime in? This has to be possible some how...

    Comment


    • #3
      I could be wrong but it looks like your service isn't using the javaMailSender you defined as a bean and instead you are using a new JavaMailSenderImpl each time (which isn't pre-configured like your bean). I would start looking there

      Comment


      • #4
        Hi thank you for the reply. This bean does work when I send out a SimpleMailMessage. But that object can't send out HTML email. Other than that I really don't understand what you mean. This is my first Spring/Hibernate/Bean project so I'm still learning.

        Thanks.

        Comment


        • #5
          Looking at line 1 of your service code you are creating a new instance of JavaMailSender by calling new JavaMailSenderImpl() and not using the bean called "javaMailSender" that you have created in your application context. You need to inject the javaMailSender bean into your service and use it and not create a new instance of JavaMailSender within the service method.

          Comment


          • #6
            jmoore_aus has it exactly right. You need to inject the instance of (or do a bean lookup for) the Spring managed JavaMailSenderImpl.

            Remember, Spring manages the lifecycle of beans (create, destroy, etc). If you do a new outside of Spring's configuration, you are on your own . Also most if not all Spring beans are Spring's version of a "Singleton".

            Comment


            • #7
              jmoore thank you for your help. I did what you said and you were like Chris says 100% correct. Sorry for late reply but I was doing other stuff before I got back to this issue. Thanks for your help!

              Comment


              • #8
                mocchi, glad you got it sorted out!

                Comment

                Working...
                X