Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 18 July 2013

Diacritics problem in Spring web app and solution

I had a diacritics problem in Spring Web application. The XML file is stored in UTF-8 and the XML parser read it properly with UTF-8 encoding - if I log the texts just read, it shows fine with diacritics in the log. However, when I display it in the web page, it shows some strange '?' which means the diacritics can not be passed properly.

Here is the solution:

1. Put encodingFilter as the first filter in web.xml:
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2. Put 'UTF-8' in both contentType and pageEncoding
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

It solved my problem!

Wednesday, 11 July 2012

Java Mail does not set a subject problem (Conflicting with openejb)

Considering the following code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test{
    private static Properties props = System.getProperties();

    public static void main(String[] args) throws Exception {
        String     host  = "xxxx";
        String     port = "xx";
        String     user = "xxxx";
        String     from = "xxxx";
        String     pass = "xxxx";
        String     text = "Test mail";
        String     to   = "xxxx";
       
        javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null); 
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session); 
        message.setFrom(new javax.mail.internet.InternetAddress(from)); 
        message.addRecipient(javax.mail.Message.RecipientType.TO, new javax.mail.internet.InternetAddress(to));
        message.setSubject("Test subject"); 
        message.setText(text); 
               
        javax.mail.Transport transport = session.getTransport("smtp"); 
        transport.connect(host, Integer.parseInt(port), user, pass);
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close();
    }
}

It works fine as a standalone Java app, but not working with Apache openejb. When I commented out this dependency in Maven pom.xml, it works again :-)
       <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5.0-1</version>
            <scope>provided</scope>
        </dependency>

A related post is here: http://bit.ly/NSIoyx. The text is copied below:
----------------------------------------------------------------------------------

Strange javamail behaviour inside tomcat

In the series: 'weird problems you'd rather not spend your valuable time on' today I present a strange javamail/tomcat related problem and its solution.

While preparing the next version of our java web app I noticed that emails sent by our app were missing the mail subject. Moreover the message appeared to get sent as plain text instead of HTML so the message body was displaying ugly html. 

While debugging everything seemed OK and the javamail API (invoked via Spring) was invoked with the correct parameters and a non-null subject.

So then I wrote a jUnit test to further isolate the problem and of course the unit test, invoking the same server-side java code as before, worked like a charm: the subject was present and the message body was interpreted as HTML.

I was now faced with a configuration problem because the exact same code was working fine from a unit test but was failing when executing from within Tomcat. After some googling I found the advice to check the classpath for duplicate or conflicting javamail implementations. I use the very handy maven command:


mvn dependency:tree

which shows the full dependency tree of your referenced libraries including implicit references, i.e. a jar required for one of my own dependencies. Then I noticed that axis-2 uses a geronimo-javamail implementation; in addition to the 'standard' javax.mail javamail. Sure enough when I excluded this implicit dependency like so:
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.4.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-activation_1.1_spec</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-javamail_1.4_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

the mail got sent correctly.





Thursday, 26 April 2012

Send mail via SSL in Java by using gmail smtp server

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailSSL {
    public static void main(String[] args) throws Exception {
        String host = "smtp.gmail.com"; 
        String from = "xxxx@gmail.com"; 
        String pass = "PASSWORD"; 
       
        Properties props = System.getProperties(); 
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host); 
        props.put("mail.smtp.user", from); 
        props.put("mail.smtp.password", pass); 
        props.put("mail.smtp.port", "587"); 
        props.put("mail.smtp.auth", "true"); 
   
        String[] to = {"xxxx@hotmail.com", "xxxx@gmail.com"}; 
   
        Session session = Session.getDefaultInstance(props, null); 
        MimeMessage message = new MimeMessage(session); 
        message.setFrom(new InternetAddress(from)); 
   
        InternetAddress[] toAddress = new InternetAddress[to.length]; 
   
        for( int i=0; i < to.length; i++ ) {  
            toAddress[i] = new InternetAddress(to[i]); 
        } 
    
        for( int i=0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
        } 
        message.setSubject("sending in a group"); 
        message.setText("Welcome to JavaMail"); 
        Transport transport = session.getTransport("smtp"); 
        transport.connect(host, from, pass); 
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close();

        System.out.println("Done.");
    }
}