【问题标题】:Is there any configuration SMTP like SpecifiedPickupDirectory in Java Mail?Java Mail 中是否有类似 SpecifiedPickupDirectory 的配置 SMTP?
【发布时间】:2019-02-21 02:42:03
【问题描述】:

在 .NET 中,我们可以配置输出文件夹电子邮件,而不是像这样发送它们。

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
    </smtp>
  </mailSettings>
</system.net>

是否可以在 Java 中为 SpecifiedPickupDirectory 等电子邮件配置输出文件夹?

【问题讨论】:

    标签: java email smtp jakarta-mail


    【解决方案1】:
    public static void createMessage(String to, String from, String subject, String body, List<File> attachments) {
        try {
            Message message = new MimeMessage(Session.getInstance(System.getProperties()));
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            // create the message part 
            MimeBodyPart content = new MimeBodyPart();
            // fill message
            content.setText(body);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(content);
            // add attachments
            for(File file : attachments) {
                MimeBodyPart attachment = new MimeBodyPart();
                DataSource source = new FileDataSource(file);
                attachment.setDataHandler(new DataHandler(source));
                attachment.setFileName(file.getName());
                multipart.addBodyPart(attachment);
            }
            // integration
            message.setContent(multipart);
            // store file
            message.writeTo(new FileOutputStream(new File("c:/mail.eml")));
        } catch (MessagingException ex) {
            Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    基于此链接的解决方案 Create an email object in java and save it to file

    【讨论】:

      【解决方案2】:

      没有。

      您可以使用 JavaMail 将文件保存到文件夹中而不是发送它们,例如,使用 Message.writeTo 方法。

      或者您可以编写一个 JavaMail 传输提供程序,将文件保存到文件夹中而不是发送它们。

      但是 JavaMail 中没有任何内置功能可以做到这一点。

      【讨论】:

      • 你们有样品吗?
      • 究竟是什么样本?你自己尝试了什么?
      猜你喜欢
      • 2020-02-13
      • 2011-02-28
      • 2016-11-11
      • 2023-04-02
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多