gpt4 book ai didi

java - 为什么我的 javamail 程序不通过 jdeveloper 运行?

转载 作者:行者123 更新时间:2023-11-30 11:02:34 25 4
gpt4 key购买 nike

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

MimeMessage message = new MimeMessage(mailSession);

printStackTrace() 显示此异常:

06-09 10:54:43.146: D/JVM(16075): [SEVERE - oracle.adfmf.framework - Utility - invoke] InvocationTargetException Error: ERROR [oracle.adfmf.framework.exception.AdfException] - Error invoking method 'sendMail' in 'class beans.UtilBean'

06-09 12:14:07.466: I/System.out(3760): [SEVERE - oracle.adfmf.framework - adf.mf.internal - logError] Request: {"classname":"oracle.adfmf.framework.api.Model","method":"processBatchRequests","params":[false,[{"classname":"oracle.adfmf.framework.api.Model","method":"evaluateMethodExpression","params":["#{UtilBean.sendMail}",[{".type":"oracle.adfmf.amx.event.ActionEvent"}],"void",["oracle.adfmf.amx.event.ActionEvent"]]}]]} exception: {"message":"Error invoking method 'sendMail' in 'class beans.UtilBean'",".exception":true,"severity":"ERROR",".type":"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);
}
}
}

在电子邮件类中,我在创建“MimeMessage”类对象之前和之后添加了打印语句,我能够在 logcat 中看到第一次打印,但看不到第二次打印。

最佳答案

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

关于java - 为什么我的 javamail 程序不通过 jdeveloper 运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30724075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com