質問

What is the Java analogue for:

thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user thufir
+OK
pass password
+OK Logged in.
stat
+OK 16 84695
retr 1
+OK 4978 octets
Return-Path: <thufir@dur.bounceme.net>
X-Original-To: thufir@dur
Delivered-To: thufir@dur
Received: from dur.bounceme.net (localhost [127.0.0.1])
    (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))
    (No client certificate requested)
    by dur.bounceme.net (Postfix) with ESMTPS id E6A58180508
    for <thufir@dur>; Sun, 26 Aug 2012 06:48:47 -0700 (PDT)
Message-Id: <1027505969.1345988926766.JavaMail.thufir@dur.bounceme.net>
To: thufir@dur
Subject: Google Developers Expert: recognizing and rewarding top developers
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_2465937.1345988926695"
Date: Sun, 26 Aug 2012 06:48:47 -0700 (PDT)
From: thufir@dur.bounceme.net

------=_Part_0_2465937.1345988926695
Content-Type: text/html
Content-Transfer-Encoding: 7bit

<img height="80" src="http://2.bp.blogspot.com/-vC8YT1LrWbw/UAW2oUAlvXI/AAAAAAAABt8/Xp5ZDiHi6JQ/s1600/
...
------=_Part_0_2465937.1345988926695--

.
quit
+OK Logging out.
Connection closed by foreign host.
thufir@dur:~$ 

I'm getting:

init:
Deleting: /home/thufir/NetBeansProjects/leafnode_postfix/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/leafnode_postfix/build/built-jar.properties
Compiling 1 source file to /home/thufir/NetBeansProjects/leafnode_postfix/build/classes
compile:
run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 1 groups in 27ms
Show INBOX for thufir@localhost
Exception in thread "main" javax.mail.NoSuchProviderException: Invalid protocol: null
    at javax.mail.Session.getProvider(Session.java:468)
    at javax.mail.Session.getStore(Session.java:546)
    at javax.mail.Session.getStore(Session.java:531)
    at javax.mail.Session.getStore(Session.java:520)
    at net.bounceme.dur.leafnode_postfix.MailClient.checkInbox(Unknown Source)
    at net.bounceme.dur.leafnode_postfix.Main.readMail(Unknown Source)
    at net.bounceme.dur.leafnode_postfix.Main.<init>(Unknown Source)
    at net.bounceme.dur.leafnode_postfix.Main.main(Unknown Source)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Presumably the problem is that I'm not logging into the dovecot POP3 server correctly? How do I pass login credentials?

package net.bounceme.dur.leafnode_postfix;

import java.io.IOException;
import static java.lang.System.out;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailClient extends Authenticator {

    public static final int SHOW_MESSAGES = 1;
    public static final int CLEAR_MESSAGES = 2;
    public static final int SHOW_AND_CLEAR =
            SHOW_MESSAGES + CLEAR_MESSAGES;
    protected String from;
    protected Session session;
    protected PasswordAuthentication authentication;

    public MailClient(UserHost userHost) {
        String user = userHost.getUser();
        String host = userHost.getHost();
        boolean debug = userHost.isDebug();
        from = user + '@' + host;
        authentication = new PasswordAuthentication(user, user);
        Properties props = new Properties();
        props.put("mail.user", user);
        props.put("mail.host", host);
        props.put("mail.debug", debug ? "true" : "false");
        props.put("mail.store.protocol", "pop3");
        props.put("mail.transport.protocol", "smtp");
        session = Session.getDefaultInstance(props);
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return authentication;
    }

    public void sendMessage(Message post) throws MessagingException, IOException {
        Message message = new MimeMessage(session);
        InternetAddress address = new InternetAddress("thufir@dur");
        message.setRecipient(Message.RecipientType.TO, address);
        message.setSubject(post.getSubject());
        Multipart mp = new MimeMultipart();
        BodyPart part = new MimeBodyPart();
        part.setContent(post.getContent(), "text/html");
        mp.addBodyPart(part);
        message.setContent(mp);
        Transport.send(message);
    }

    public void checkInbox(int mode)
            throws MessagingException, IOException {
        if (mode == 0) {
            return;
        }
        boolean show = (mode & SHOW_MESSAGES) > 0;
        boolean clear = (mode & CLEAR_MESSAGES) > 0;
        String action =
                (show ? "Show" : "")
                + (show && clear ? " and " : "")
                + (clear ? "Clear" : "");
        out.println(action + " INBOX for " + from);
        Store store = session.getStore();
        store.connect();
        out.println(store.getDefaultFolder());
        Folder root = store.getDefaultFolder();
        Folder inbox = root.getFolder("inbox");
        inbox.open(Folder.READ_WRITE);
        Message[] msgs = inbox.getMessages();
        if (msgs.length == 0 && show) {
            System.out.println("No messages in inbox");
        }
        for (int i = 0; i < msgs.length; i++) {
            MimeMessage msg = (MimeMessage) msgs[i];
            if (show) {
                System.out.println("    From: " + msg.getFrom()[0]);
                System.out.println(" Subject: " + msg.getSubject());
                System.out.println(" Content: " + msg.getContent());
            }
            if (clear) {
                msg.setFlag(Flags.Flag.DELETED, true);
            }
        }
        inbox.close(true);
        store.close();
        System.out.println();
    }
}

Incidentally, sending messages to localhost, or dur, works fine. The full FQDN is dur.bounceme.net, although just dur seems to suffice in many cases. I'm just doing everything on one box, nothing over the intertubes.

役に立ちましたか?

解決

First of all, it appears the email you're sending to is invalid. You have thufir@dur, did you possibly mean something like thufir@dur.com?

Second, you need to hand the Session proper information. You need to make the following changes:

    authentication = new PasswordAuthentication(user, *pass*);//You must hand it username and pass, not two usernames like before
    Properties props = new Properties();
    props.put("mail.user", user);
    props.put("mail.host", host);
    props.put("mail.debug", debug ? "true" : "false");
    props.put("mail.store.protocol", pop3Provider);//an example provider is pop.gmail.com, or I think aol is pop.aol.com, many follow that pattern, this is used for reading inbox/incoming messages (hence the store)
    props.put("mail.transport.protocol", smtpProvider);//follows same as above, but smtp not pop, this is used for outgoing messages (hence the transport)
    session = Session.getDefaultInstance(props);

I think that's your main issue, tell me how that works

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top