簡體   English   中英

從ofice365帳戶發送電子郵件時Java錯誤

[英]Error in Java while sending email from ofice365 account

從ofice365帳戶發送電子郵件時Java錯誤。

javax.mail.AuthenticationFailedException:535 5.7.3身份驗證失敗[MA1PR01CA0090.INDPRD01.PROD.OUTLOOK.COM]

這是我的代碼。 電子郵件和密碼正確。 請幫我。

    final String username = StaticParameters.adminEmail;
    final String password = StaticParameters.adminPassword;

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.office365.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(body, "text/html; charset=utf-8");
        Transport.send(message);
        System.out.println("Done");
        return true;

    } catch (MessagingException e) {

        throw new RuntimeException(e);
    }

嘗試這個:

 private static Properties props;
    private static Session session;
    static {
        props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.host", "m.outlook.com");
        props.put("mail.smtp.auth", "true");
        session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("office365 email address"
                        "office365 password");
            }
        });

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM