为什么我的javamail程序没有通过jdeveloper运行?

时间:2015-06-09 06:02:24

标签: java javamail jdeveloper hybrid-mobile-app oracle-maf

我正在开发Oracle MAF应用程序并使用Java mail API发送Outlook会议请求。我可以从java命令行成功发送邮件,但是当使用移动应用程序尝试相同时,代码不会在创建“MimeMessage”类对象的地方之后。

MimeMessage message = new MimeMessage(mailSession);

printStackTrace()显示此异常:

  

06-09 10:54:43.146:D / JVM(16075):[SEVERE - oracle.adfmf.framework -   实用程序 - 调用] InvocationTargetException错误:错误   [oracle.adfmf.framework.exception.AdfException] - 错误调用   'class beans.UtilBean'中的方法'sendMail'

     

06-09 12:14:07.466:I / System.out(3760):[严重 -   oracle.adfmf.framework - adf.mf.internal - logError]请求:   { “类名”: “oracle.adfmf.framework.api.Model”, “方法”: “processBatchRequests”, “PARAMS”:[假,[{ “类名”: “oracle.adfmf.framework.api.Model”, “方法”: “evaluateMethodExpression”, “PARAMS”:[ “#{UtilBean.sendMail}”,[{ “类型。 ”:“ oracle.adfmf.amx.event.ActionEvent ”}],“ 空隙”,[“预言.adfmf.amx.event.ActionEvent“]]}]]}   exception:{“message”:“在类中调用方法'sendMail'时出错   。beans.UtilBean'”, “异常”:真 “严重性”: “ERROR”, “类型”: “oracle.adfmf.framework.exception.AdfException”}

Full source code of my class is here:

package beans;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import oracle.adfmf.amx.event.ActionEvent;

public class Email {

    private static final String SMTP_HOST_NAME = "mail_host_name";
    private static final String SMTP_AUTH_USER = "smtp_user_id";
    private static final String SMTP_AUTH_PWD = "smtp_auth_pass";
    private static final String SMTP_AUTH_PORT = "smtp_port";
    private static final String MAIL_FROM_ADDRESS = "from_addr";
    private static final String RECEIPENT_1 = "receipent_1_addr";
    private static final String RECEIPENT_2 = "receipent_2_addr";

    public void sendEmail(ActionEvent actionEvent) {

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", SMTP_AUTH_PORT);

        Authenticator auth = new SMTPAuthenticator();

        //Get the session object
        Session mailSession = Session.getDefaultInstance(props, auth);

        //compose message
        try {
            System.out.print("CALLED0"); //I can see this in Logcat Logs
            MimeMessage message = new MimeMessage(mailSession); // Program stuck here
            System.out.print("CALLED1");//I can not see this in Logcat Logs
            message.addHeaderLine("method=REQUEST");
            message.addHeaderLine("charset=UTF-8");
            message.addHeaderLine("component=vevent");
            message.setFrom(new InternetAddress(MAIL_FROM_ADDRESS));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(RECEIPENT_1));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(RECEIPENT_2));
            message.setSubject("Outlook Meeting Request Using JavaMail");

            StringBuffer sb = new StringBuffer();
            StringBuffer buffer =
                sb.append("BEGIN:VCALENDAR\n" + "PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN\n" +
                          "VERSION:2.0\n" + "METHOD:REQUEST\n" + "BEGIN:VEVENT\n" +
                          "ATTENDEE;CN=\"Attendee1\";ROLE=REQ-PARTICIPANT;MAILTO:attendee1@addendant.com\n" +
                          "ATTENDEE;CN=\"Attendee2\";ROLE=OPT-PARTICIPANT;MAILTO:attendee2@addendant.com\n" +
                          "ORGANIZER:MAILTO:org@org.com\n" + "DTSTART:20050406T073000Z\n" +
                          "DTEND:20050406T080000Z\n" + "LOCATION:conf\n" + "TRANSP:OPAQUE\n" + "SEQUENCE:0\n" +
                          "UID:040000008200E00074C5B7101A82E00800000000A0A742E5073AC5010000000000000000100\n" +
                          " 0000029606C073D82204AB6C77ACE6BC2FBE2\n" + "DTSTAMP:20050405T122004Z\n" +
                          "CATEGORIES:Meeting\n" + "DESCRIPTION:What are you doing?\n\n" + "SUMMARY:How are you?\n" +
                          "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "BEGIN:VALARM\n" + "TRIGGER:PT1440M\n" +
                          "ACTION:DISPLAY\n" + "DESCRIPTION:Reminder\n" + "END:VALARM\n" + "END:VEVENT\n" +
                          "END:VCALENDAR\n");
            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
            messageBodyPart.setHeader("Content-ID", "calendar_message");
            messageBodyPart.setContent(buffer.toString(), "text/calendar");
            // Create a Multipart
            Multipart multipart = new MimeMultipart();
            // Add part one
            multipart.addBodyPart(messageBodyPart);
            // Put parts in message
            message.setContent(multipart);

            //send message
            Transport.send(message);
            System.out.println("message sent successfully");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}

在Email类中,我在创建“MimeMessage”类对象之前和之后添加了print语句,我能够在logcat中看到第一个打印而不是第二个打印。

1 个答案:

答案 0 :(得分:1)

在Web逻辑服务器上部署java项目作为Web服务(作为.war包)对我有用。