Olá pessoal. Estou tendo dificuldade para melhorar um método de envio de arquivo e por isso preciso da ajuda de vocês.
A ideia é fazer o upload de um arquivo e logo em seguida envia-lo por email.
utilizando o código abaixo, consigo fazer isso desde que salve o arquivo em disco antes.
Queria não precisar salvar o arquivo em disco e depois lê-lo novamente para anexar no email.
Alguém tem alguma ideia de como anexar o arquivo no email diretamente da memoria?
[code]public void carregarArquivo(FileUploadEvent event)
throws FileNotFoundException, IOException {
String caminho = "c:\\" + event.getFile().getFileName();
byte[] conteudo = event.getFile().getContents();
FileOutputStream fos = new FileOutputStream(caminho);
fos.write(conteudo);
fos.close();
try {
send("xxx@xxx.com", "yyyy@yyy.com", "teste", null, caminho);
} catch (Exception ex) {
Logger.getLogger(ServicosController.class.getName()).log(Level.SEVERE, null, ex);
}
}
//envio de email
@Resource(name = "mail/simpes/default")
private Session mailSession;
public void send(String from, String to, String subject, String body, String fileAttachment) throws Exception {
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setText("Test mail one");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName("curriculo");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart, "text/html");
Address toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
Transport.send(message);
}[/code]
valeu a força.