Java说邮件已经发送了,但是实际上还没有

时间:2018-07-31 13:00:37

标签: java javamail

我正在尝试通过Java发送邮件,但它不起作用。那也没有显示任何错误。我是javamail的新手,请帮助:)我到处都搜索了这个问题,但是我只能在php中找到这个问题。所以请让我知道如何在Java中调试它。

public void sendMail(){

        String host = "smtp.gmail.com";
        String user = "tharumudu@gmail.com";
        String password = "pass";
        String to = "tharumudu@gmail.com";
        String from = "tharumudu@gmail.com";
        String subject = "Subject";
        String messageText = "Thada ! ";
        boolean sessionBug = false;

        Properties properties = System.getProperties();

        properties.put("mail.smtp.starttls.enable","true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.required","true");
        properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

        Session session = Session.getDefaultInstance(properties, null);
        session.setDebug(sessionBug);

        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            InternetAddress address = new InternetAddress(to);
            msg.setRecipient(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);


            Transport transport = session.getTransport("smtp");
            transport.connect(host, user, password);
            transport.close();
            System.out.println("email sent successfully");

        } catch (MessagingException e) {
            System.err.println(e);
        }


    }

我在这里调用了方法

  public void sendButtonClicked(ActionEvent actionEvent) {
        sendMail();
    }

2 个答案:

答案 0 :(得分:3)

您的代码中没有调用sendsendMessageimport numpy as np from sklearn.model_selection import train_test_split from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) estimator = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0) estimator.fit(X_train, y_train) n_nodes = estimator.tree_.node_count children_left = estimator.tree_.children_left children_right = estimator.tree_.children_right feature = estimator.tree_.feature threshold = estimator.tree_.threshold # First let's retrieve the decision path of each sample. The decision_path # method allows to retrieve the node indicator functions. A non zero element of # indicator matrix at the position (i, j) indicates that the sample i goes # through the node j. node_indicator = estimator.decision_path(X_test) # Similarly, we can also have the leaves ids reached by each sample. leave_id = estimator.apply(X_test) # Now, it's possible to get the tests that were used to predict a sample or # a group of samples. First, let's make it for the sample. sample_id = 0 node_index = node_indicator.indices[node_indicator.indptr[sample_id]: node_indicator.indptr[sample_id + 1]] print('Rules used to predict sample %s: ' % sample_id) for node_id in node_index: if leave_id[sample_id] != node_id: continue if (X_test[sample_id, feature[node_id]] <= threshold[node_id]): threshold_sign = "<=" else: threshold_sign = ">" print("decision id node %s : (X_test[%s, %s] (= %s) %s %s)" % (node_id, sample_id, feature[node_id], X_test[sample_id, feature[node_id]], threshold_sign, threshold[node_id])) 是实际发送电子邮件的地方。您所要做的就是创建一条消息并建立连接。连接后,发送。

答案 1 :(得分:3)

您必须致电import React from 'react'; import chai from 'chai'; import chaiEnzyme from 'chai-enzyme'; import {shallow} from 'enzyme'; import configureStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import sinon from 'sinon' import {Provider} from 'react-redux'; import App from './App'; const expect = chai.use(chaiEnzyme()).expect const mockStore = configureStore([thunk]); const wrap = (initialState, props) => { return shallow(<Provider store={mockStore(initialState)}><App {...props} /></Provider>) }; describe('App container', () => { it('validates properties', () => { const stub = sinon.stub(console, 'warn'); console.warn.reset(); React.createElement(App, {}); expect(stub.calledOnce).to.equal(true); expect(stub.calledWithMatch(/Failed prop type/)).to.equal(true); console.warn.restore(); }); it('renders without crashing', () => { wrap(); }); it('is react-redux connected', () => { const wrapper = wrap(); expect(wrapper.find('Connect(App)')).to.have.length(1); }); it('correctly maps properties', () => { const wrapper = wrap({images: []}); expect(wrapper.props().images).to.equal([]); }); }); send来发送电子邮件。所以您的以下代码

sendMessage

可以替换为

Transport transport = session.getTransport("smtp");
transport.connect(host, user, password);
transport.close();

用户名和密码已通过您的会话属性提供。