簡體   English   中英

單擊JButton時發送電子郵件?

[英]Send email when JButton is clicked?

請有人徹底幫助我,我一直在此工作。 我對編程有些陌生,並且以前從未設置過服務器或其他任何東西。 我在嘗試運行程序時遇到此錯誤。 java.net.ConnectException: Connection refused: connect ”和“ javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

這是我的代碼

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jButton1.isEnabled());
Properties sessionProperties = System.getProperties();
String to = "bleh@gmail.com";
    String from = "bleh@gmail.com"; 
    String host = "localhost"; 
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    Session session = Session.getDefaultInstance(properties);

    try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("Infomation");

     // Now set the actual message
     message.setText("Hello!");

     // Send message
     Transport.send(message);

    }catch (MessagingException mex) {
     mex.printStackTrace();

看來您的代碼來自http://www.tutorialspoint.com/java/java_sending_email.htm如果是這樣,我想將您引向tut中的句子

Here it is assumed that your localhost is connected to the internet and capable enough
to send an email.

具備能力,則意味着您有一個在本地主機上運行的SMTP服務器。 之所以收到該異常,是因為Transport嘗試在localhost:25上使用SMTP服務器發送郵件。

然后,您的選擇是在本地運行SMTP服務器(這確實很麻煩),或者使用第三方SMTP(GMail,Amazon SES等)服務來發送郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM