alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Axis 2 example source code file (MailClient.java)

This example Axis 2 source code file (MailClient.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Axis 2 tags/keywords

clear_messages, folder, io, mailclient, mailclient, messagingexception, mimemessage, mimemessage, passwordauthentication, properties, show, show_messages, show_messages, string, string, util

The Axis 2 MailClient.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.transport.mail;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;

public class MailClient extends Authenticator {
    public static final int SHOW_MESSAGES = 1;
    public static final int CLEAR_MESSAGES = 2;
    public static final int SHOW_AND_CLEAR = SHOW_MESSAGES + CLEAR_MESSAGES;
    private static final Log log = LogFactory.getLog(MailClient.class);
    protected PasswordAuthentication authentication;
    protected String from;
    protected Session session;

    public MailClient(String user, String host) {
        this(user, host, user, false);
    }

    public MailClient(String user, String host, String password) {
        this(user, host, password, false);
    }

    public MailClient(String user, String host, String password, boolean debug) {
        from = user + '@' + host;
        authentication = new PasswordAuthentication(user, password);

        Properties props = new Properties();

        props.put("mail.user", user);
        props.put("mail.host", host);
        props.put("mail.debug", debug
                ? "true"
                : "false");
        props.put("mail.store.protocol", "pop3");
        props.put("mail.transport.protocol", "smtp");
        session = Session.getInstance(props, this);
    }

    public int checkInbox(int mode) throws MessagingException, IOException {
        int numMessages = 0;

        if (mode == 0) {
            return 0;
        }

        boolean show = (mode & SHOW_MESSAGES) > 0;
        boolean clear = (mode & CLEAR_MESSAGES) > 0;
        String action = (show
                ? "Show"
                : "") + ((show && clear)
                ? " and "
                : "") + (clear
                ? "Clear"
                : "");

        log.info(action + " INBOX for " + from);

        Store store = session.getStore();

        store.connect();

        Folder root = store.getDefaultFolder();
        Folder inbox = root.getFolder("inbox");

        inbox.open(Folder.READ_WRITE);

        Message[] msgs = inbox.getMessages();

        numMessages = msgs.length;

        if ((msgs.length == 0) && show) {
            log.info("No messages in inbox");
        }

        for (int i = 0; i < msgs.length; i++) {
            MimeMessage msg = (MimeMessage) msgs[i];

            if (show) {
                log.info("    From: " + msg.getFrom()[0]);
                log.info(" Subject: " + msg.getSubject());
                log.info(" Content: " + msg.getContent());
            }

            if (clear) {
                msg.setFlag(Flags.Flag.DELETED, true);
            }
        }

        inbox.close(true);
        store.close();

        return numMessages;
    }

    public void sendMessage(String to, String subject, String content, String soapAction)
            throws MessagingException {
        log.info("SENDING message from " + from + " to " + to);

        MimeMessage msg = new MimeMessage(session);

        msg.setHeader("transport.mail.soapaction", soapAction);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject);
        msg.setText(content);
        Transport.send(msg);
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return authentication;
    }
}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 MailClient.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.