Forum Stats

  • 3,853,788 Users
  • 2,264,269 Discussions
  • 7,905,444 Comments

Discussions

Java Mail API and Swing Application Framework

843810
843810 Member Posts: 46,938
edited Jul 28, 2010 12:54PM in Java Desktop Applications
Hello, I built a Desktop Application with NetBeans IDE 6.9 using New Project / Java / Java Desktop Application (based on the Swing Application Framework JSR 296). An email has to be sent when a button ist being pressed. I use the latest Java Mail API 1.4.3. When I press the button, nothing happens but the application stops any reaction immediately and I have to kill it. The curious thing is that the same code used outside the Desktop Application Framework does work very well. Is there any incompatibility between Java Mail API and Swing Application Framework?

Here is the code, I hope for some helpful hints:


import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

public class EmailTestApp extends SingleFrameApplication {

@Override protected void startup() {
show(new EmailTestView(this));
}

public static EmailTestApp getApplication() {
return Application.getInstance(EmailTestApp.class);
}

public static void main(String[] args) {
launch(EmailTestApp.class, args);
}
}


import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailTestView extends FrameView {

public EmailTestView(SingleFrameApplication app) {
super(app);
initComponents();
}

@Action public void emailen() {
try {
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, passwd);
}
}
);
MimeMultipart mmp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText("Nachrichtentext", "UTF-8", "plain");
mmp.addBodyPart(nachrichtenText);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(addressFrom));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(addressTo));
message.setSubject(subject);
message.setSentDate(new Date());
message.setContent(mmp);

Transport.send(message);

} catch (Exception e) {
System.out.println(e);
}
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

mainPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();

mainPanel.setName("mainPanel"); // NOI18N

javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(emailtest.EmailTestApp.class).getContext().getActionMap(EmailTestView.class, this);
jButton1.setAction(actionMap.get("email")); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(emailtest.EmailTestApp.class).getContext().getResourceMap(EmailTestView.class);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jButton1)
.addContainerGap(87, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jButton1)
.addContainerGap(75, Short.MAX_VALUE))
);

setComponent(mainPanel);
}// </editor-fold>//GEN-END:initComponents

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JPanel mainPanel;
// End of variables declaration//GEN-END:variables

}

Comments

  • 843810
    843810 Member Posts: 46,938
    Sorry for one mistake:

    mmp.addBodyPart(nachrichtenText);

    should be

    mmp.addBodyPart(mbp);
  • 843810
    843810 Member Posts: 46,938
    It sounds like you are most certainly clogging up the event dispatch thread. Spawn a new thread in which to send the message.
  • 843810
    843810 Member Posts: 46,938
    Thanks for the reply. Unfortunately it doesn't send the mail even if I start a new thread:

    @Action public void emailen() {
    new Thread() {
    @Override
    public void run() {
    try {
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", host);
    properties.setProperty("mail.smtp.port", port);
    properties.setProperty("mail.smtp.auth", "true");
    Session session = Session.getInstance(properties, new Authenticator(){
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, passwd);
    }
    }
    );
    MimeMultipart mmp = new MimeMultipart();
    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setText("Nachrichtentext", "UTF-8", "plain");
    mmp.addBodyPart(mbp);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(addressFrom));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(addressTo));
    message.setSubject(subject);
    message.setSentDate(new Date());
    message.setContent(mmp);
    // the following line is reached
    System.out.println("reached");
    Transport.send(message);
    // this line is never reached
    System.out.println("never reached");
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }.start();
    }

    When I debug I find at the top of the stack: SocketInputStream.socketRead0

    Any other hints?
  • 843810
    843810 Member Posts: 46,938
    I find it strange that you claim it works fine without swing. Swing should have nothing to do with it. Have you tried sendMessage() instead of Transport.send() ?

    From the JavaMail API:

    Q: I need to authenticate to my SMTP server so I call trans.connect(host, user, password) and then trans.send(msg) to send the message, but it's not working.
    A: You should call trans.sendMessage(msg, addrs) to send the message. As described above, the send method is a static convenience method that acquires its own Transport object and creates its own connection to use for sending; it does not use the connection associated with any Transport object through which it is invoked.

    That's all I can think of, sorry. Please let me know what the problem was, if you manage to solve it.

    -Tom
  • 843810
    843810 Member Posts: 46,938
    Thanks again, Tom, for trying to help me. I finally found the problem with the proxy server.With this additional line it works:

    ProxySelector.setDefault(null);

    So I guess the Swing Application Framework initiallized the ProxySelector with some system environment variables and was left uninitialized otherwise.
This discussion has been closed.