簡體   English   中英

使用Apache Commons電子郵件在我的硬盤上發送帶有附件的郵件

[英]Send a mail with an attachment on my hard drive with Apache commons email

我在使用Apache Commons電子郵件在郵件中發送附件時遇到問題。 為了解釋它的快速和骯臟,我發送了郵件,但是當我在Outlook中查看郵件時根本沒有附件。

我使用Apache Commons Email v1.4和JAVA8。我想在硬盤驅動器上的此位置C:\\ myfolder \\ myfile.log上添加一個日志文件。

到目前為止,這是我嘗試添加的附件

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
   File rejLogFile = new File(logRejetPath.toString());
   email.attach(new FileDataSource(rejLogFile), "test", "test");                
}
email.send();

要么

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
   File rejLogFile = new File(logRejetPath.toString());
   email.attach(rejLogFile);                
}
email.send();

要么

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(logRejetPath.toString());
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("test");
    attachment.setName("test");
    email.attach(attachment);              
}
email.send();

我精確的電子郵件是這樣創建的MultiPartEmail對象:

MultiPartEmail email = new MultiPartEmail();

    try {
        email.setHostName(config.getSmtpHost()); 
        email.setSmtpPort(Integer.valueOf(config.getSmtpPort()));
        if (!config.getSmtpUser().isEmpty()) {
            email.setAuthenticator(
                    new DefaultAuthenticator(config.getSmtpUser(), config.getSmtpPwd()));
            email.setSSLOnConnect(true);
        } else {
            email.setSSLOnConnect(false);
        }
        email.setCharset("utf-8");
        email.setFrom("me@me.fr");
        email.setSubject("subjectforemail");
        email.setContent(this.getMessage(), "text/html");

        final String[] destinataires = config.getMailDestinataires().split(";");
        for (final String dest : destinataires) {
            email.addTo(dest);
        }

每次使用這些不同的方法添加附件時,我都會收到帶有郵件的電子郵件,但沒有附件。 每次變量pathExists為TRUE,每次我都沒有錯誤。

感謝您日后的回答和幫助。

編輯:通過更改此找到的解決方案:

MultiPartEmail email = new MultiPartEmail();

這樣 :

HtmlEmail email = new HtmlEmail();

通過更改此方法找到的解決方案:

MultiPartEmail email = new MultiPartEmail();

這樣 :

HtmlEmail email = new HtmlEmail();

暫無
暫無

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

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