有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在试图找到一个更新到最新版本的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);

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


共 (2) 个答案

  1. # 1 楼答案

    使用Spring框架邮件服务试试这个。这对我很有用:

    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);
    
  2. # 2 楼答案

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

    String username = "classified";
    

    致:

    String username = "classified@yahoo.com";
    

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

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