Example usage for javax.mail MessagingException printStackTrace

List of usage examples for javax.mail MessagingException printStackTrace

Introduction

In this page you can find the example usage for javax.mail MessagingException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:mitm.common.tools.SendMail.java

public static void main(String[] args) {
    PropertyConfigurator.configure("conf/tools.log4j.properties");

    try {// w  w w.  ja va  2s .c  om
        new SendMail(args);
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (MissingArgumentException e) {
        logger.warn("Not all required parameters are specified. " + e);
    } catch (ParseException e) {
        logger.error("Command line parsing error. " + e);
    } catch (FileNotFoundException e) {
        logger.error("File cannot be found. " + e);
    } catch (Exception e) {
        logger.error("Unknown error.", e);
    }
}

From source file:MainClass.java

public static void main(String argv[]) {
    int msgnum = -1;
    int optind;/*from w  w  w  .j  a va2  s  .  c  o  m*/
    InputStream msgStream = System.in;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-v")) {
            verbose = true;
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            mbox = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-p")) {
            port = Integer.parseInt(argv[++optind]);
        } else if (argv[optind].equals("-s")) {
            showStructure = true;
        } else if (argv[optind].equals("-S")) {
            saveAttachments = true;
        } else if (argv[optind].equals("-m")) {
            showMessage = true;
        } else if (argv[optind].equals("-a")) {
            showAlert = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
            System.out.println("\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]");
            System.out.println("or     msgshow -m [-v] [-D] [-s] [-S] [-f msg-file]");
            System.exit(1);
        } else {
            break;
        }
    }

    try {
        if (optind < argv.length)
            msgnum = Integer.parseInt(argv[optind]);

        // Get a Properties object
        Properties props = System.getProperties();

        // Get a Session object
        Session session = Session.getInstance(props, null);
        session.setDebug(debug);

        if (showMessage) {
            MimeMessage msg;
            if (mbox != null)
                msg = new MimeMessage(session, new BufferedInputStream(new FileInputStream(mbox)));
            else
                msg = new MimeMessage(session, msgStream);
            dumpPart(msg);
            System.exit(0);
        }

        // Get a Store object
        Store store = null;
        if (url != null) {
            URLName urln = new URLName(url);
            store = session.getStore(urln);
            if (showAlert) {
                store.addStoreListener(new StoreListener() {
                    public void notification(StoreEvent e) {
                        String s;
                        if (e.getMessageType() == StoreEvent.ALERT)
                            s = "ALERT: ";
                        else
                            s = "NOTICE: ";
                        System.out.println(s + e.getMessage());
                    }
                });
            }
            store.connect();
        } else {
            if (protocol != null)
                store = session.getStore(protocol);
            else
                store = session.getStore();

            // Connect
            if (host != null || user != null || password != null)
                store.connect(host, port, user, password);
            else
                store.connect();
        }

        // Open the Folder

        Folder folder = store.getDefaultFolder();
        if (folder == null) {
            System.out.println("No default folder");
            System.exit(1);
        }

        if (mbox == null)
            mbox = "INBOX";
        folder = folder.getFolder(mbox);
        if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
        }

        // try to open read/write and if that fails try read-only
        try {
            folder.open(Folder.READ_WRITE);
        } catch (MessagingException ex) {
            folder.open(Folder.READ_ONLY);
        }
        int totalMessages = folder.getMessageCount();

        if (totalMessages == 0) {
            System.out.println("Empty folder");
            folder.close(false);
            store.close();
            System.exit(1);
        }

        if (verbose) {
            int newMessages = folder.getNewMessageCount();
            System.out.println("Total messages = " + totalMessages);
            System.out.println("New messages = " + newMessages);
            System.out.println("-------------------------------");
        }

        if (msgnum == -1) {
            // Attributes & Flags for all messages ..
            Message[] msgs = folder.getMessages();

            // Use a suitable FetchProfile
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);
            fp.add(FetchProfile.Item.FLAGS);
            fp.add("X-Mailer");
            folder.fetch(msgs, fp);

            for (int i = 0; i < msgs.length; i++) {
                System.out.println("--------------------------");
                System.out.println("MESSAGE #" + (i + 1) + ":");
                dumpEnvelope(msgs[i]);
                // dumpPart(msgs[i]);
            }
        } else {
            System.out.println("Getting message number: " + msgnum);
            Message m = null;

            try {
                m = folder.getMessage(msgnum);
                dumpPart(m);
            } catch (IndexOutOfBoundsException iex) {
                System.out.println("Message number out of range");
            }
        }

        folder.close(false);
        store.close();
    } catch (Exception ex) {
        System.out.println("Oops, got exception! " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}

From source file:msgshow.java

public static void main(String argv[]) {
    int optind;//from  w w  w  .j  av a2  s  .c o  m
    InputStream msgStream = System.in;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-v")) {
            verbose = true;
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            mbox = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-p")) {
            port = Integer.parseInt(argv[++optind]);
        } else if (argv[optind].equals("-s")) {
            showStructure = true;
        } else if (argv[optind].equals("-S")) {
            saveAttachments = true;
        } else if (argv[optind].equals("-m")) {
            showMessage = true;
        } else if (argv[optind].equals("-a")) {
            showAlert = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
            System.out.println("\t[-P password] [-f mailbox] [msgnum ...] [-v] [-D] [-s] [-S] [-a]");
            System.out.println("or     msgshow -m [-v] [-D] [-s] [-S] [-f msg-file]");
            System.exit(1);
        } else {
            break;
        }
    }

    try {
        // Get a Properties object
        Properties props = System.getProperties();

        // Get a Session object
        Session session = Session.getInstance(props, null);
        session.setDebug(debug);

        if (showMessage) {
            MimeMessage msg;
            if (mbox != null)
                msg = new MimeMessage(session, new BufferedInputStream(new FileInputStream(mbox)));
            else
                msg = new MimeMessage(session, msgStream);
            dumpPart(msg);
            System.exit(0);
        }

        // Get a Store object
        Store store = null;
        if (url != null) {
            URLName urln = new URLName(url);
            store = session.getStore(urln);
            if (showAlert) {
                store.addStoreListener(new StoreListener() {
                    public void notification(StoreEvent e) {
                        String s;
                        if (e.getMessageType() == StoreEvent.ALERT)
                            s = "ALERT: ";
                        else
                            s = "NOTICE: ";
                        System.out.println(s + e.getMessage());
                    }
                });
            }
            store.connect();
        } else {
            if (protocol != null)
                store = session.getStore(protocol);
            else
                store = session.getStore();

            // Connect
            if (host != null || user != null || password != null)
                store.connect(host, port, user, password);
            else
                store.connect();
        }

        // Open the Folder

        Folder folder = store.getDefaultFolder();
        if (folder == null) {
            System.out.println("No default folder");
            System.exit(1);
        }

        if (mbox == null)
            mbox = "INBOX";
        folder = folder.getFolder(mbox);
        if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
        }

        // try to open read/write and if that fails try read-only
        try {
            folder.open(Folder.READ_WRITE);
        } catch (MessagingException ex) {
            folder.open(Folder.READ_ONLY);
        }
        int totalMessages = folder.getMessageCount();

        if (totalMessages == 0) {
            System.out.println("Empty folder");
            folder.close(false);
            store.close();
            System.exit(1);
        }

        if (verbose) {
            int newMessages = folder.getNewMessageCount();
            System.out.println("Total messages = " + totalMessages);
            System.out.println("New messages = " + newMessages);
            System.out.println("-------------------------------");
        }

        if (optind >= argv.length) {
            // Attributes & Flags for all messages ..
            Message[] msgs = folder.getMessages();

            // Use a suitable FetchProfile
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);
            fp.add(FetchProfile.Item.FLAGS);
            fp.add("X-Mailer");
            folder.fetch(msgs, fp);

            for (int i = 0; i < msgs.length; i++) {
                System.out.println("--------------------------");
                System.out.println("MESSAGE #" + (i + 1) + ":");
                dumpEnvelope(msgs[i]);
                // dumpPart(msgs[i]);
            }
        } else {
            while (optind < argv.length) {
                int msgnum = Integer.parseInt(argv[optind++]);
                System.out.println("Getting message number: " + msgnum);
                Message m = null;

                try {
                    m = folder.getMessage(msgnum);
                    dumpPart(m);
                } catch (IndexOutOfBoundsException iex) {
                    System.out.println("Message number out of range");
                }
            }
        }

        folder.close(false);
        store.close();
    } catch (Exception ex) {
        System.out.println("Oops, got exception! " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}

From source file:Main.java

private static boolean isReplySign(MimeMessage msg) {
    boolean replySign = false;
    try {//  w  w  w  .  j  a v a 2  s  . com
        String[] header = msg.getHeader("Disposition-Notification-To");
        if (header != null) {
            replySign = true;
        }
    } catch (MessagingException e) {
        e.printStackTrace();
    } finally {
        return replySign;
    }
}

From source file:edu.hawaii.soest.hioos.storx.StorXEmailUtil.java

/**
 * Move messages from the parent backlog folder to year-month folders based
 * on the date sent of the message//w w  w  . j  av  a 2s  . c  om
 * 
 * @param targetMailbox 
 * @param sourceMailbox 
 */
public static void organizeMessages(String sourceMailbox, String targetMailbox) {
    ArrayList<String> years = getYears();
    ArrayList<String> months = getMonths();

    try {
        targetParentFolder = mailStore.getFolder(targetMailbox);
        targetParentFolder.open(Folder.READ_WRITE);
        log.debug("Target folder: " + targetParentFolder.getName());

        sourceParentFolder = mailStore.getFolder(sourceMailbox);
        sourceParentFolder.open(Folder.READ_WRITE);
        log.debug("Source folder: " + sourceParentFolder.getName());

        Message messages[];
        while (!targetParentFolder.isOpen()) {
            targetParentFolder.open(Folder.READ_WRITE);

        }
        messages = (Message[]) sourceParentFolder.getMessages();
        log.debug("Processing " + messages.length + " messages.");

        int count = 0;
        for (Message message : messages) {
            Date sentDate = message.getSentDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            String folderByDate = formatter.format(sentDate);

            Folder destinationFolder = targetParentFolder.getFolder(folderByDate);
            targetParentFolder.copyMessages(new Message[] { message }, destinationFolder);
            //message.setFlag(Flags.Flag.DELETED, true);
            count++;
            log.debug(count + ")" + message.getSentDate() + " moved to\t" + destinationFolder.getFullName());
        }

    } catch (MessagingException e) {
        e.printStackTrace();

    } finally {
        try {
            mailStore.close();

        } catch (MessagingException e) {
            e.printStackTrace();

        }
    }

}

From source file:com.sat.common.CustomReport.java

/**
 * Sendmail./*w w  w.  java 2s.  com*/
 * 
 * @param msg
 *            the msg
 * @throws AddressException
 *             the address exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void sendmail(String msg) throws AddressException, IOException {

    ConfigFileHandlerManager miscConfigFileHandlerManager = new ConfigFileHandlerManager();
    miscConfigFileHandlerManager.loadPropertiesBasedonPropertyFileName("com.cisco.lms.lms");
    Validate miscValidate = new Validate();

    String from = miscValidate.readsystemvariable("MAIL.FROM");
    String host = miscValidate.readsystemvariable("MAIL.SMTP.HOST");
    String to = miscValidate.readsystemvariable("MAIL.TO");

    String subject = miscValidate.readsystemvariable("MAIL.SUBJECT");
    String personal = miscValidate.readsystemvariable("MAIL.FROM.PERSONAL");

    // Mail to details
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    javax.mail.Session session = javax.mail.Session.getDefaultInstance(properties);

    // compose the message
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from, personal));

        message.addRecipients(Message.RecipientType.TO, to);
        message.setSubject(subject);
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(msg, "text/html");
        mp.addBodyPart(htmlPart);
        message.setContent(mp);
        Transport.send(message);
        System.out.println(msg);
        System.out.println("message sent successfully....");

    } catch (MessagingException mex) {
        mex.printStackTrace();
    } catch (Exception e) {
        System.out.println("\tException in sending mail to configured mailers list");
    }
}

From source file:com.cisco.dbds.utils.report.CustomReport.java

/**
 * Sendmail./*w ww.ja v a  2 s . c o m*/
 *
 * @param msg the msg
 * @throws AddressException the address exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void sendmail(String msg) throws AddressException, IOException {
    //Properties CustomReport_CONFIG = new Properties();
    //FileInputStream fn = new FileInputStream(System.getProperty("user.dir")+ "/src/it/resources/config.properties");
    //CustomReport_CONFIG.load(fn);
    String from = "automationreportmailer@cisco.com";
    // String from = "sitaut@cisco.com";
    //String host = System.getProperty("MAIL.SMTP.HOST");
    String host = "outbound.cisco.com";
    // Mail to details
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    // String ecsip = CustomReport_CONFIG.getProperty("ecsip");
    javax.mail.Session session = javax.mail.Session.getDefaultInstance(properties);

    // compose the message
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from
        // ));
                , "Automation Report Mailer"));
        //message.addRecipients(Message.RecipientType.TO, System.getProperty("MAIL.TO"));
        //message.setSubject(System.getProperty("mail.subject"));

        message.addRecipients(Message.RecipientType.TO, "maparame@cisco.com");
        message.setSubject("VCS consle report");
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(msg, "text/html");
        mp.addBodyPart(htmlPart);
        message.setContent(mp);
        Transport.send(message);
        System.out.println(msg);
        System.out.println("message sent successfully....");

    } catch (MessagingException mex) {
        mex.printStackTrace();
    } catch (Exception e) {
        System.out.println("\tException in sending mail to configured mailers list");
    }
}

From source file:org.sipfoundry.voicemail.VmMessage.java

public static VmMessage newMessageFromMime(Mailbox mailbox, IMAPMessage msg) {
    VmMessage me = new VmMessage();

    // Generate the next message ID
    me.m_messageId = ExtMailStore.GetMsgId(msg);
    if (me.m_messageId == null) {
        me.m_messageId = nextMessageId(mailbox.getMailstoreDirectory() + "/..");
    }// w  w  w. j a va2s  .c  om

    // Generate the MessageDescriptor;
    me.m_messageDescriptor = new MessageDescriptor();
    me.m_messageDescriptor.setId(mailbox.getUser().getIdentity());

    try {
        String str = msg.getHeader("X-SIPX-FROMURI", ";");
        me.m_messageDescriptor.setFromUri(str);

        me.m_messageDescriptor.setDurationSecs(msg.getSize());
        me.m_unHeard = !msg.isSet(Flag.SEEN);
        me.m_messageDescriptor.setTimestamp(msg.getReceivedDate().getTime());
        me.m_messageDescriptor.setSubject(msg.getSubject());

        Priority pr = Priority.NORMAL;

        if (msg.isSet(Flag.FLAGGED)) {
            pr = Priority.URGENT;
            me.m_urgent = true;

        }
        me.m_messageDescriptor.setPriority(pr);

    } catch (MessagingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    String baseName = mailbox.getInboxDirectory() + me.m_messageId + "-00";
    me.m_audioFile = new File(baseName + ".wav");
    me.m_descriptorFile = new File(baseName + ".xml");
    me.m_statusFile = new File(baseName + ".sta");
    me.m_urgentFile = new File(baseName + ".urg");
    String operation = "storing stuff";
    try {
        if (me.m_unHeard) {
            operation = "creating status file " + me.m_statusFile.getPath();
            LOG.debug("VmMessage::newMessage " + operation);
            FileUtils.touch(me.m_statusFile);
        }

        if (me.m_urgent) {
            operation = "creating urgent file " + me.m_urgentFile.getPath();
            LOG.debug("VmMessage::newMessage " + operation);
            FileUtils.touch(me.m_urgentFile);
        }

        operation = "creating messageDescriptor " + me.m_descriptorFile.getPath();
        LOG.debug("VmMessage::newMessage " + operation);
        new MessageDescriptorWriter().writeObject(me.m_messageDescriptor, me.m_descriptorFile);
    } catch (IOException e) {
        LOG.error("VmMessage::newMessage error while " + operation, e);
        return null;
    }
    LOG.info("VmMessage::newMessage created message " + me.m_descriptorFile.getPath());
    if (me.m_unHeard) {
        Mwi.sendMWI(mailbox);
    }

    return me;
}

From source file:isl.FIMS.utils.Utils.java

public static boolean sendEmail(String to, String subject, String context) {
    boolean isSend = false;

    try {//from   ww  w .  ja v  a2 s . c o m

        String host = "smtp.gmail.com";
        String user = emailAdress;
        String password = emailPass;

        String port = "587";
        String from = "no-reply-" + systemName + "@gmail.com";
        Properties props = System.getProperties();

        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        //  props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.EnableSSL.enable", "true");
        //     props.put("mail.smtp.socketFactory.port", port);
        //    props.put("mail.smtp.socketFactory.class",
        //            "javax.net.ssl.SSLSocketFactory");
        //    props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props, null);
        //session.setDebug(true);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        // To get the array of addresses
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        message.setSubject(subject, "UTF-8");
        message.setContent(context, "text/html; charset=UTF-8");
        Transport transport = session.getTransport("smtp");
        try {
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            transport.close();
        }
        isSend = true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return isSend;

}

From source file:net.indialend.attendance.compnent.Email.java

/**
 * This method triggers the email in background thread i.e. the mail is sent
 * asynchronously//from  w w  w  .  java  2  s  . co m
 *
 * @param from Email from which mail is triggered
 * @param to Email to whom mail has to be sent
 * @param subject Subject of the email Messages
 * @param msg Message body to be sent along with email
 * @throws InterruptedException
 */
@Async
public void sendMail(String from, String to, String subject, String msg) throws InterruptedException {
    try {

        MimeMessage message = mailSender.createMimeMessage();
        message.setSubject(subject);
        message.setHeader("Content-Type", "text/plain; charset=UTF-8");
        MimeMessageHelper helper;
        helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setText(msg, true);
        mailSender.send(message);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}