Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the attached file left open? #76

Open
fdabrao opened this issue Jul 29, 2016 · 6 comments
Open

Is the attached file left open? #76

fdabrao opened this issue Jul 29, 2016 · 6 comments

Comments

@fdabrao
Copy link

fdabrao commented Jul 29, 2016

I used this code and after finish I can´t delete the attached file, any idea?
(send-message {:host "smtp.gmail.com" :user usuario :pass senha :tls :yes :port 587} {:from usuario :to destino :subject "Reports" :body [{:type "text/html" :content (clojure.string/replace (slurp template) #"\$mes" mes)} {:type :inline :content (java.io.File. anexo) :content-type "application/pdf"}]})

@drewr
Copy link
Owner

drewr commented Jul 29, 2016

Is this on Windows?

@fdabrao
Copy link
Author

fdabrao commented Jul 29, 2016

Yes, on Windows

@charles-dyfis-net
Copy link
Collaborator

I don't believe the JVM guarantees a timeline for GC. If you use a try/finally block to force the file to close, does that resolve the issue?

(BTW, Windows is the only modern operating system I know of where open files can't be unlinked; typically it's just a reference count decrement).

@fdabrao
Copy link
Author

fdabrao commented Jul 29, 2016

I tried this too:
( let [anexo (java.io.File. "file.pdf")] (send-message {:host "smtp.gmail.com" :user usuario :pass senha :tls :yes :port 587} {:from usuario :to destino :subject "Reports" :body [{:type "text/html" :content (clojure.string/replace (slurp template) #"\$mes" mes)} {:type :inline :content anexo :content-type "application/pdf"}]} (.close anexo)) , same result

@drewr
Copy link
Owner

drewr commented Jul 29, 2016 via email

@fdabrao
Copy link
Author

fdabrao commented Jul 29, 2016

So, I tried to rewrite it like:

(defn make-auth [user pass]
    (proxy (javax.mail.Authenticator) []
        (getPasswordAuthentication [] (PasswordAuthentication. user pass))))
(defn enviar-email-anexo [host porta usuario senha anexo destinatario]
    (let [prop (Properties.)
                _ (.put prop "mail.smtp.host" host) 
                _ (.put prop "mail.smtp.port" porta)
                _ (.put prop"mail.smtp.auth" true)
                _ (.put prop "mail.smtp.starttls.enable" true)
                _ (.put prop "mail.user" usuario)
                _ (.put prop "mail.password" senha)
                auth (make-auth usuario senha)
                sessao (Session/getInstance prop auth)
                mensagem (MimeMessage. sessao)
                messageBodyPart (MimeBodyPart.)
                multipart (MimeMultipart.)
                attachPart (MimeBodyPart.)
                ]
        (.setFrom mensagem (InternetAddress. usuario))
        (.setRecipient mensagem Message$RecipientType/TO (InternetAddress. destinatario))
        (.setSubject mensagem "Relatório de Service Desk")

        (.setContent messageBodyPart "<html><body>Hello</body></html>" "text/html")
        (.addBodyPart multipart messageBodyPart)

        (.attachFile attachPart anexo)
        (.addBodyPart multipart attachPart)

        (.setContent mensagem multipart)
        (Transport/send mensagem)
        ))

and it´s not blocking the file. So I think in some situation your code keep the file open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants