gpt4 book ai didi

JavaMail Yandex 发件人

转载 作者:行者123 更新时间:2023-12-01 06:04:07 27 4
gpt4 key购买 nike

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

我的代码如下:

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 服务的凭据。

最佳答案

        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);

关于JavaMail Yandex 发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com