Example usage for javax.mail Message setContent

List of usage examples for javax.mail Message setContent

Introduction

In this page you can find the example usage for javax.mail Message setContent.

Prototype

public void setContent(Object obj, String type) throws MessagingException;

Source Link

Document

A convenience method for setting this part's content.

Usage

From source file:org.bimserver.webservices.Service.java

@Override
public void sendCompareEmail(SCompareType sCompareType, SCompareIdentifier sCompareIdentifier, Long poid,
        Long roid1, Long roid2, String address) throws ServerException, UserException {
    DatabaseSession session = bimServer.getDatabase().createSession();
    try {/* www.  j  a  v a  2 s.c om*/
        SUser currentUser = getCurrentUser(session);
        Revision revision1 = session.get(StorePackage.eINSTANCE.getRevision(), roid1, false, null);
        Revision revision2 = session.get(StorePackage.eINSTANCE.getRevision(), roid2, false, null);
        String senderName = currentUser.getName();
        String senderAddress = currentUser.getUsername();
        if (!senderAddress.contains("@") || !senderAddress.contains(".")) {
            senderAddress = bimServer.getSettingsManager().getSettings().getEmailSenderAddress();
        }

        Session mailSession = bimServer.getMailSystem().createMailSession();

        Message msg = new MimeMessage(mailSession);

        try {
            InternetAddress addressFrom = new InternetAddress(senderAddress);
            addressFrom.setPersonal(senderName);
            msg.setFrom(addressFrom);

            InternetAddress[] addressTo = new InternetAddress[1];
            addressTo[0] = new InternetAddress(address);
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            msg.setSubject("BIMserver Model Comparator");
            SCompareResult compareResult = compare(roid1, roid2, sCompareType, sCompareIdentifier);
            String html = CompareWriter.writeCompareResult(compareResult, revision1.getId(), revision2.getId(),
                    sCompareType, getProjectByPoid(poid), false);
            msg.setContent(html, "text/html");
            Transport.send(msg);
        } catch (AddressException e) {
            throw new UserException(e);
        } catch (UnsupportedEncodingException e) {
            throw new UserException(e);
        } catch (MessagingException e) {
            throw new UserException(e);
        }
    } finally {
        session.close();
    }
}