Java send mail in text and html

We posted earlier how send email using java email api.
Here is example of sending email in plain text and html format in same email.

  //get mail session
        Properties props = new Properties();
        props.put("mail.smtp.host", "localhost");
        Session session = session.getDefaultInstance(props);

        // create the messge.
        MimeMessage mimeMessage = new MimeMessage(session);

        MimeMultipart rootMixedMultipart = new MimeMultipart("mixed");
        mimeMessage.setContent(rootMixedMultipart);

        MimeMultipart nestedRelatedMultipart = new MimeMultipart("related");
        MimeBodyPart relatedBodyPart = new MimeBodyPart();
        relatedBodyPart.setContent(nestedRelatedMultipart);
        rootMixedMultipart.addBodyPart(relatedBodyPart);

        MimeMultipart messageBody = new MimeMultipart("alternative");
        MimeBodyPart bodyPart = null;
        for (int i = 0; i < nestedRelatedMultipart.getCount(); i++) {
            BodyPart bp = nestedRelatedMultipart.getBodyPart(i);
            if (bp.getFileName() == null) {
                bodyPart = (MimeBodyPart) bp;
            }
        }
        if (bodyPart == null) {
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            nestedRelatedMultipart.addBodyPart(mimeBodyPart);
            bodyPart = mimeBodyPart;
        }
        bodyPart.setContent(messageBody, "text/alternative");

        // Create the plain text part of the message.
        MimeBodyPart plainTextPart = new MimeBodyPart();
        plainTextPart.setText("This is plain text message", "UTF-8");
        messageBody.addBodyPart(plainTextPart);

        // Create the HTML text part of the message.
        MimeBodyPart htmlTextPart = new MimeBodyPart();
        htmlTextPart.setContent("<h1>This is plain HTML message", "text/html;charset=UTF-8");
        messageBody.addBodyPart(htmlTextPart);

        mimeMessage.setReplyTo(new InternetAddress[]{new InternetAddress("[email protected]")});
        mimeMessage.setFrom(new InternetAddress("[email protected]"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        mimeMessage.setSentDate(new Date());
        mimeMessage.setSubject("Mixed message");
        Transport.send(mimeMessage);

Using Collections.copy for list

Using java Collections to copy array list

I have seen lot’s of people using Collections.copy and most of times you see this error on console

Exception in thread "main" java.lang.IndexOutOfBoundsException:
Source does not fit in dest.

Normally we use following code to copy an array-list

ArrayList<String> items = new ArrayList<String>();

items.add("item 1");
items.add("item 2");
items.add("item 6");

ArrayList<String> items2= new ArrayList<String>(items.size());
Collections.copy(items2,items);

Only problem in the above code is that for list items2 we are assuming that it has same capacity as list items. If you see the code carefully, it is only setting up initial capacity not size and Collections api check for size instead of capacity of list and it result to exception.

IMO there should be an improvement in collection api to copy items, based on capacity, instead of size. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752 .

Solution

You can use any of following option to copy a list

ArrayList<String> items2= new ArrayList<String>(items);
ArrayList<String> items2 = (ArrayList<String>)items.clone();

References

http://java.sun.com/javase/6/docs/api/java/util/Collections.html#copy(java.util.List,%20java.util.List)
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752

Ldap configuration in Jetspeed 2.2.0

Ldap configuration in Jetspeed 2 (JS2) is very easy. Its only two required steps to configure Ldap in JS2.

1) Enable Ldap module
First you need to create “spring-filter-key.properties” under jetspeed/WEB-INF/conf and add the following lines

#Turn on LDAP Security
spring.filter.key = portal.ldap

Spring-filter-key. properties can be used to enable or disable different module of jetspeed.

2) Define Ldap Connection propetries

Jetspeed store all its Configuration settings in jetspeed.properties.

Following are default jetspeed ldap settings
jetspeedldap
First four points are self explanatory.

ldap.context.factory
Jetspeed uses ldap to communicate to ldap server. I think it would not be advisable to changes context factory until its rally required.

ldap.user.filter =

You can define you own object class for users

ldap.search.scope

Ldap search scope refer to searching objects in ldap sub tree. I would suggest not-to change this value

ldap.user.searchBase
ldap.role.searchBase
ldap.group.searchBase
You can define tree location where all you users reside. Once you define user search base of user, Jetspeed will only look for user and there relation under that node.
Search base can beĀ  defined for groups and roles too.

ldap user entryPrefix

In ldap there are different user classes to create user and they have there own different respective schema, So you can choose you can define your own uid prefix