java - 如何直接从Java代码发送带有附件的私有(private)公司电子邮件?

标签 java outlook

我很好奇是否有任何方法可以直接从 Java 代码发送带有附件的电子邮件。该电子邮件属于公司 mylogin@companyxxx.com`,即使用 Java 操作 Outlook?或者也许我不需要操作Outlook,有登录名和密码等人员就足够了...

最佳答案

是的,您可以使用 javamail 来做到这一点,您可以从here下载jar 。对于 Outlook,您只需按如下方式设置属性:

    Properties props = new Properties();
    props.put("mail.smtp.user", username);
    props.put("mail.smtp.host", "smtp.live.com");
    props.put("mail.smtp.port", "25");
    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.EnableSSL.enable", "true");
    props.setProperty("mail.smtp.port", "587");
    props.setProperty("mail.smtp.socketFactory.port", "587");

或者如果您想要完整的示例,请使用以下方法:

    public int sendMailWithAttachment(String to, String subject, String body, String filepath, String sendFileName) {
    final String username = "YOUR EMAIL";
    final String password = "YOUR PWD";
    Properties props = new Properties();
    props.put("mail.smtp.user", username);
    props.put("mail.smtp.host", "smtp.live.com");
    props.put("mail.smtp.port", "25");
    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.EnableSSL.enable", "true");
    props.setProperty("mail.smtp.port", "587");
    props.setProperty("mail.smtp.socketFactory.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.setText(body);
        BodyPart messageBodyPart = new MimeBodyPart();
        Multipart multipart = new MimeMultipart();
        BodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("<html><body>HELLO</body></html>", "text/html");
        DataSource source = new FileDataSource(filepath);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(sendFileName);
        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(htmlPart);
        message.setContent(multipart);
        Transport.send(message);
        return 1;
    } catch (Exception e) {
        return 0;
    }
}

关于java - 如何直接从Java代码发送带有附件的私有(private)公司电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311366/

相关文章:

java - 正则表达式查找合格的变量使用

java - 选择具有多个选项的覆盖方法java

java - 如何修复 docker-compose 中 Jedis 中的连接被拒绝错误?

vba - 使用 VBA 将 Outlook 类别分配给 MailItem 时遇到问题

c# - OOF 回复的字体

html - Windows Outlook 0 7' 10' html 电子邮件中图像上的垂直 1px

java - 如何断言是否从下拉列表中选择了所选选项。 Selenium 。页面对象模型

java - 准备好的语句返回 false 但行已插入?

c# - 与登录用户以外的用户一起使用兑换 (Outlook) - 并收到错误

outlook - Zapier:修改 webhook 监听器 HTTP 响应?