Results 1 to 2 of 2
  1. #1
    fhamumtaz is offline Member
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Default Connection Failed (Email Client)

    I've got some error for connection email server. I think the problem is in connection, because always failed for downloading message from server.

    Part of my code :

    // Connect to e-mail server.
    public void connect() {
    // Display connect dialog.
    ConnectDialog dialog = new ConnectDialog(this);
    dialog.show();

    // Build connection URL from connect dialog settings.
    StringBuffer connectionUrl = new StringBuffer();
    connectionUrl.append(dialog.getTypes() + "://");
    connectionUrl.append(dialog.getUsername() + ":");
    connectionUrl.append(dialog.getPassword() + "@");
    connectionUrl.append(dialog.getServer() + "/");

    /* Display dialog stating that messages are
    currently being downloaded from server. */
    final DownloadingDialog downloadingDialog =
    new DownloadingDialog(this);
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    downloadingDialog.show();
    }
    });

    // Establish JavaMail session and connect to server.
    Store store = null;
    try {
    // Initialize JavaMail session with SMTP server.
    Properties props = new Properties();
    props.put("mail.smtp.host", dialog.getSmtpServer());
    session = Session.getDefaultInstance(props, null);

    // Connect to e-mail server.
    URLName urln = new URLName(connectionUrl.toString());
    store = session.getStore(urln);
    store.connect();
    } catch (Exception e) {
    // Close the downloading dialog.
    downloadingDialog.dispose();

    // Show error dialog.
    showError("Unable to connect.", true);
    }

    // Download message headers from server.
    try {
    // Open main "INBOX" folder.
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    // Get folder's list of messages.
    Message[] messages = folder.getMessages();

    // Retrieve message headers for each message in folder.
    FetchProfile profile = new FetchProfile();

    profile.add(FetchProfile.Item.ENVELOPE);
    folder.fetch(messages, profile);

    // Put messages in table.
    tableModel.setMessages(messages);
    } catch (Exception e) {
    // Close the downloading dialog.
    downloadingDialog.dispose();

    // Show error dialog.
    showError("Unable to download messages.", true);
    }

    // Close the downloading dialog.
    downloadingDialog.dispose();
    }

    // Show error dialog and exit afterwards if necessary.
    private void showError(String message, boolean exit) {
    JOptionPane.showMessageDialog(this, message, "Error",
    JOptionPane.ERROR_MESSAGE);
    if (exit)
    System.exit(0);
    }

    // Get a message's content.
    public static String getMessageContent(Message message)
    throws Exception {
    Object content = message.getContent();
    if (content instanceof Multipart) {
    StringBuffer messageContent = new StringBuffer();
    Multipart multipart = (Multipart) content;
    for (int i = 0; i < multipart.getCount(); i++) {
    Part part = (Part) multipart.getBodyPart(i);
    if (part.isMimeType("text/plain")) {
    messageContent.append(part.getContent().toString() );
    }
    }
    return messageContent.toString();
    } else {
    return content.toString();
    }
    }

    // Run the E-mail Client.
    public static void main(String[] args) {
    EmailClient2 client = new EmailClient2();
    client.show();

    // Display connect dialog.
    client.connect();
    }
    }

    When i run my application :

    server:pop.gmail.com
    username:********@gmail.com
    password:*************
    smtp server:smtp.gmail.com

    and my error message "Unable to connect".
    The exception does not appear to know what would happen with the error.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Connection Failed (Email Client)

    When you catch an exception you need to log the stack trace.
    Even a simple:
    Java Code:
    e.printStackTrace();
    would help. That way you know exactly what the error is, and where it happened.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 07-21-2013, 09:24 PM
  2. Exception Handling for failed connection
    By jmorr212 in forum Networking
    Replies: 0
    Last Post: 02-25-2011, 05:48 AM
  3. JDBC connection failed to oracle 10g can anyone help
    By Navatha in forum Advanced Java
    Replies: 1
    Last Post: 11-11-2010, 08:58 AM
  4. email client
    By gvpproject in forum AWT / Swing
    Replies: 4
    Last Post: 03-01-2010, 12:33 PM
  5. Replies: 2
    Last Post: 01-24-2010, 03:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •