如何通过Java从Outlook发送电子邮件?

Jen*_*aiz 3 java apache email outlook jakarta-mail

我被企业防火墙所困,该防火墙不允许我通过Java Mail API或Apache Commons Email之类的传统方式向组织内部的其他人发送电子邮件(无论如何,这都是我想要的)。但是我的Outlook 2010显然可以发送这些电子邮件。我想知道是否有一种方法可以通过Java代码自动执行Outlook 2010,以便Outlook可以发送电子邮件?我知道可以使用“ mailto”之类的东西弹出带有默认填充信息的默认Outlook发送对话框,但是我正在寻找一种使发送操作在幕后进行的方法。感谢您提供任何信息。

Inz*_* IT 5

您可以javamail使用Outlook官方网站上描述的配置通过Outlook发送电子邮件

这是小的演示代码

public static void main(String[] args) {
    final String username = "your email";  // like yourname@outlook.com
    final String password = "*********";   // password here

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

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

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("receiver mail"));   // like inzi769@gmail.com
        message.setSubject("Test");
        message.setText("HI you have done sending mail with outlook");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)


注意:我用Javamail API 1.5.6