如何通过JavaMail从Yahoo服务器发送电子邮件?

时间:2012-10-20 15:59:54

标签: java email smtp javamail yahoo

我一直在努力寻找一个更新到最新版本的JavaMail的网站,但每当我尝试时,我都会遇到这个恼人的错误(启用调试)
帮助?

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "pop.mail.yahoo.com", port 110, isSSL false
S: +OK hello from popgate-0.8.0.357900 pop001.mail.ir2.yahoo.com 
C: CAPA
S: +OK CAPA list follows
IMPLEMENTATION popgate-0.8.0.357900
XOIP
EXPIRE-NEVER
PIPELINING
RESP-CODES
TOP
UIDL
USER
SASL LOGIN PLAIN
STLS
.
DEBUG POP3: PIPELINING enabled
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command failed
C: QUIT
S: +OK
javax.mail.AuthenticationFailedException: [AUTH] Access to this service is not permitted.
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at dong.pong.ping.Client.main(Client.java:42)

代码:

String smtpHost = "smtp.mail.yahoo.com";
        String popHost = "pop.mail.yahoo.com";
        String from = "classified@yahoo.com";
        String to = "classified@yahoo.com";
        String username = "classified";
        String password = "secret";

        // Get system properties
        Properties props = System.getProperties();

        // Setup mail server
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", 587);

        // Get session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true);

        Store store = session.getStore("pop3");
        store.connect(popHost, username, password);

        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to));
        message.setSubject("Hello JavaMail");
        message.setText("Welcome to Yahoo's JavaMail");

        // Send message
        Transport.send(message);

如果你们有人有工作代码,你可以发布吗?感谢

4 个答案:

答案 0 :(得分:2)

您有高级帐户吗?

  

或者这是雅虎问题吗?

     

移至相应的论坛,但某些版本的雅虎(特别是yahoo.com)不支持POP3访问邮件,除非您已注册高级帐户或拥有合作伙伴服务(如AT& T / Yahoo)。当它显示“不允许访问”时,这可能意味着什么 - 您没有高级帐户。

[AUTH] Access to this service is not permitted

编辑:另请参阅Java Mail: Unable to send email via Yahoo了解工作示例。

答案 1 :(得分:0)

根据我见过的各种网站,雅虎的SMTP端口是465


实际上,现在我查看堆栈跟踪,看起来当您尝试连接到POP3存储时出现问题。如果你只是想发送电子邮件,我不知道为什么你需要连接到POP3。

答案 2 :(得分:0)

我已检查this page,您的用户名似乎必须是完整地址,因此您必须更改:

String username = "classified";

要:

String username = "classified@yahoo.com";

使用TLS / SSL,雅虎的SMTP是465。

 props.put("mail.smtp.port", 465);

答案 3 :(得分:0)

使用Spring Framework Mail服务尝试这个。它对我有用:

Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.mail.yahoo.com");
        mailSender.setPort(587);
        mailSender.setUsername("username1");
        mailSender.setPassword("password");
        mailSender.setJavaMailProperties(props);

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(email);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);