Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在JavaFX中使用SMTP发送电子邮件_Java - Fatal编程技术网

Can';t在JavaFX中使用SMTP发送电子邮件

Can';t在JavaFX中使用SMTP发送电子邮件,java,Java,当我在JavaFx中按下按钮发送电子邮件时,会打开一个QuantumToolKit.class选项卡,应用程序将挂起 该按钮可以与其他功能配合使用 SMTP代码 package application; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import

当我在JavaFx中按下按钮发送电子邮件时,会打开一个QuantumToolKit.class选项卡,应用程序将挂起 该按钮可以与其他功能配合使用

SMTP代码

    package application;
import java.util.Properties;

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.swing.JOptionPane;

public class EmailController {
    String reciever;
    String subj;
    String con;
    final String username;
    final String password;
    String from;
    String host;
    String put_auth;
    String put_ttls;
    String put_host;
    String put_port;
    public EmailController (){
        username="username";
        password="passwrd";
        from="example";
        host="smtp.gmail.com";
        reciever="reciever_example";
        subj="Subject";
        con="Content";
        put_auth="mail.smtp.auth";
        put_ttls="mail.smtp.strarttls.enable";
        put_host="mail.smtp.host";
        put_port="mail.smtp.port";
    }
    public void send()
    {

        Properties props=new Properties();
        props.put(put_auth, "true");
        props.put(put_ttls, "true");
        props.put(put_host, host);
        props.put(put_port, "587");

        Session session=Session.getInstance(props,new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication () {
                return new PasswordAuthentication(username,password);
            }
        });
        try {
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(reciever));
            message.setSubject(subj);
            message.setText(con);
            Transport.send(message);
            JOptionPane.showMessageDialog(null, "Done");
        }catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
控制器类代码中的#onAction函数

public void send_Email(){
    sendm.send();     //sendm is an object from class EmailController 
}
仅此按钮的FXML代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="557.0" prefWidth="604.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Heart">
   <children>
      <Button layoutX="88.0" layoutY="307.0" mnemonicParsing="false" onAction="#send_Email" prefHeight="26.0" prefWidth="356.0" text="Send email" />
   </children>
</AnchorPane>

我已经修好了,我只是把starttls写错了

        put_ttls="mail.smtp.strarttls.enable";
所以我必须把它改成

 put_ttls="mail.smtp.starttls.enable";