简体   繁体   English

单击JButton时发送电子邮件?

[英]Send email when JButton is clicked?

Someone please please be thorough and help me with this, I've been at this forever now. 请有人彻底帮助我,我一直在此工作。 I'm somewhat new to programming and I've never set up a server or anything before. 我对编程有些陌生,并且以前从未设置过服务器或其他任何东西。 I get this error trying to run my program. 我在尝试运行程序时遇到此错误。 " java.net.ConnectException: Connection refused: connect " and " javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; " java.net.ConnectException: Connection refused: connect ”和“ javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

Here's my code : 这是我的代码

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();

It looks like your code came from http://www.tutorialspoint.com/java/java_sending_email.htm If so, I'd like to direct you to the sentence in the tut 看来您的代码来自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.

By capable, it means that you have an SMTP server running on localhost; 具备能力,则意味着您有一个在本地主机上运行的SMTP服务器。 the reason why you got the exception was because Transport attempted to use an SMTP server on localhost:25 to send the mail. 之所以收到该异常,是因为Transport尝试在localhost:25上使用SMTP服务器发送邮件。

Your options are then to either run an SMTP server locally (a real pain) or to use a third-party SMTP (GMail, Amazon SES, etc.) service to send the mail. 然后,您的选择是在本地运行SMTP服务器(这确实很麻烦),或者使用第三方SMTP(GMail,Amazon SES等)服务来发送邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM