我试图在我的JavaFX应用程序中发送一封电子邮件,一切工作正常,电子邮件被发送到收件人,当我在IDE (InteliJ)中运行它时,没有出现异常和错误,但当我在InteliJ之外运行该应用程序时,它不工作。我制作了一个简单的登记表,将字段中的数据保存到我的数据库中:
String username;
String password;
String email;
Random rd = new Random();
int ID;
public void registerUser(javafx.event.ActionEvent ab) {
username = textUser.getText();
password = textPass.getText();
email = textEmail.getText();
ID = rd.nextInt(999999999);
Connection connectt = null;
try {
Class.forName("org.sqlite.JDBC");
connectt = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\barte\\OneDrive\\Desktop\\sqlite databases\\PRODUCTS\\Products.db");
String s = "INSERT INTO Users(Username,Password,Email,UserID) VALUES (?,?,?,?) ";
PreparedStatement registera = connectt.prepareStatement(s);
registera.setString(1, username);
registera.setString(2, password);
registera.setString(3, email);
registera.setInt(4, ID);
System.out.println(username);
System.out.println(password);
System.out.println(email);
registera.executeUpdate();
System.out.println("Added to Database");
sendMail();
registerr.setStyle("-fx-background-color: #69ff59;");
registerr.setText("Check Your MailBox");
registerr.setOnMouseClicked(event -> {
registerr.setText("Email Has been sent");
});
textUser.setText(null);
textEmail.setText(null);
textPass.setText(null);
regiPane.setVisible(false);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
下面是发送电子邮件的代码:
public void sendMail() throws MessagingException {
String USER_NAME = "stoc****";
String from = USER_NAME;
String PASSWORD = "************";
String pass = PASSWORD;
String RECIPT = textEmail.getText();
String TOPIC = "Welcome " + username + "!";
String BODY = "Dear user! " +
"You can sign into StockFX by your ID/Username and password" +
"User ID: " + ID + "\n" + "Password: " + password + "\n" +
"We would like to thank you for using our services now and in future!";
String[] to = {RECIPT};
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"****", PASSWORD);
}
});
MimeMessage message = new MimeMessage(session);
try {
try {
message.setFrom(new InternetAddress(from));
} catch (MessagingException e) {
e.printStackTrace();
}
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
try {
toAddress[i] = new InternetAddress(to[i]);
} catch (AddressException e) {
e.printStackTrace();
}
}
for (int i = 0; i < toAddress.length; i++) {
try {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
} catch (MessagingException e) {
e.printStackTrace();
}
}
try {
message.setSubject(TOPIC);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.setText(BODY);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.saveChanges();
} catch (MessagingException e) {
e.printStackTrace();
}
registerr.setStyle("-fx-background-color: #69ff59;");
registerr.setText("You can now log in");
registerr.setDisable(false);
textUser.setText(null);
textEmail.setText(null);
textPass.setText(null);
regiPane.setVisible(false);
Transport transport = session.getTransport("smtp");
System.out.println("get protocl");
transport.connect(host, from, pass);
System.out.println("get host,from and password");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("get recipients");
transport.close();
System.out.println("close");
System.out.println("Email Sent Successfully!");
} finally {
System.out.println("Complete Process");
}
}
在InteliJ中一切正常,但是void sendEmail在runable jar中不工作。进口:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
此类是控制器类
这是主类:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage UI) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("DashBoard.fxml"));
UI.setTitle("DIREXT SCANNER (DEMO VER 0.5)");
UI.setScene(new Scene(root, 800, 600));
UI.initStyle(StageStyle.UNDECORATED);
UI.setResizable(false);
UI.show();
UI.setFullScreenExitHint("Press 'ESC' to exit full screen");
}
public static void main(String[] args){
launch(args);
}
}
我尝试重建项目,再次删除和添加库,但相同的结果 有人遇到同样的问题吗?是与 IDE 相关还是缺少导入或方法?我试图在论坛上寻找类似的问题,我已经修复了一些问题,因为以前可运行的 jar 根本无法运行。如果存在问题,请有人提供链接。
编辑这是我从PowerShell运行jar时遇到的错误:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Node.fireEvent(Unknown Source)
at javafx.controls/javafx.scene.control.Button.fire(Unknown Source)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
... 52 more
Caused by: java.lang.NoClassDefFoundError: javax/activation/DataHandler
at sample.DashBoardController.registerUser(DashBoardController.java:321)
... 62 more
Caused by: java.lang.ClassNotFoundException: javax.activation.DataHandler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 63 more
如果你使用的是外部库,那么当你解压到jar文件时,问题有时会发生在这个库的路径上,简单的解决方法是创建一个新的项目,然后创建新的文件作为旧的项目,然后移动它们
多亏了你们的帮助,我找到了解决方案。在项目结构中,我删除了所有工件、模块和库,并再次添加所有内容,然后重新构建项目,然后构建工件,我还将项目文件夹设置为SourceRoot,并成功了。
大家好,我正在尝试使用JavaMail和Amazon SES发送电子邮件,这是我写的代码, 但我有个例外说, 线程“main”javax中出现异常。邮政SendFailedException:无法发送电子邮件;嵌套的例外是:com。亚马逊。服务。简单邮件。模型MessageRejectedException:未验证电子邮件地址。以下身份未通过US-EAST-1区域的登记 我没有得到任何解决方案,s
我想用Java发送一封邮件,但使用我在http://www.tutorialspoint.com/Java/java_sending_email.htm找到的这篇泰文教程是行不通的。 这里是我的主要方法 我有以下错误: javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.connectException:
本文向大家介绍基于java使用JavaMail发送邮件,包括了基于java使用JavaMail发送邮件的使用技巧和注意事项,需要的朋友参考一下 一、邮件的相关概念 邮件协议。主要包括: SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件 POP3协议:Post Office Protocol 3,即邮局协议的第三个版本,用于接收邮件 IM
这是我发送邮件的代码 我在tomcat的lib文件夹中添加了和。我将eclipse与Tomcat7、JRE7和Struts2一起使用。通过Java发送邮件时出现以下控制台错误:
我有一个Outlook.com的电子邮件帐户。我希望我的Java应用程序能够使用JavaMail从该电子邮件帐户发送电子邮件。为此,我通过OAuth授予了我的应用程序访问权限。 我已经能够使用msgshow中提供的精彩示例应用程序通过IMAP成功连接.java。但是,我正在尝试通过SMTP通过我的 Outlook.com 电子邮件帐户发送电子邮件,但我没有任何运气。我尝试过同时使用msgsend.
我需要通过我的应用程序发送一封电子邮件,比如使用带有OAuth的javamail API,但我需要如何使用我在下面添加的代码。 我的代码: