首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaMail Yandex发送器

JavaMail Yandex发送器
EN

Stack Overflow用户
提问于 2018-03-26 18:54:47
回答 2查看 2.4K关注 0票数 2

我正在建立一个应用程序来发送和接收使用JavaMail的Java与Yandex的电子邮件。当我发送电子邮件时,我发现了一个问题,我不知道如何解决它。它只是弹出并错误地显示为535 5.7.8 Error: authentication failed: Invalid user or password!

我的代码如下:

代码语言:javascript
复制
public static void sendFromYandex(String from, String pass, String to, String subject, String body) throws AddressException, MessagingException {
    Properties props = System.getProperties();
    String host = "smtp.yandex.com";
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.quitwait", "false");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.debug", "true");

    Session session = Session.getInstance(props);
    session.setDebug(true);
    MimeMessage message = new MimeMessage(session);
    message.setHeader("Content-Type", "text/plain; charset=UTF-8");
    message.setSubject(subject, "UTF-8");
    message.setText(body, "UTF-8");

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress toAddress = new InternetAddress(to);
        message.addRecipient(Message.RecipientType.TO, toAddress);

        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    } catch (AddressException ae) {
    } catch (MessagingException me) {
    }
}

frompass是登录Yandex服务的凭证。

EN

回答 2

Stack Overflow用户

发布于 2019-11-22 18:54:58

代码语言:javascript
复制
        String from = "your_mail_address@yandex.com";
        String pass = "yourmail_password";
        String[] to = { "to_mail_address@****.com" };
        String host = "smtp.yandex.com";
        Properties props = System.getProperties();

        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.quitwait", "false");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props, null);
票数 0
EN

Stack Overflow用户

发布于 2018-12-23 09:05:48

即使您的用户名和密码正确,也可能发生这种情况。在Google中有一个叫做“不太安全的应用程序”,搜索它并打开它。我相信Yandex应该有类似的东西。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49489767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档