Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] Anexar um arquivo TXT usando Javamail



Em 23 de julho de 2014 15:28, Henrique Melo <henrique1985@gmail.com> escreveu:
Vou colar um codigo aqui


Qlqr bronca eu explico..


public static boolean enviarEmail(List<String> emails, String localArquivo,
String mensagemEmail, String tituloEmail, String remetente)
throws Exception {

Boolean retorno = false;
String from = remetente;

String host = "192.168.1.3";

String filename = localArquivo;

String msgText1 = mensagemEmail;
String subject = tituloEmail;

// cria algumas propriedades e obtem uma sessao padrao
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("port", "25");

Session session = Session.getInstance(props, null);

// cria a mensagem
MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));

// Destinatarios
Integer aux = emails.size();

InternetAddress[] address = new InternetAddress[aux];
aux = 0;
for (String email : emails) {
address[aux] = new InternetAddress(email.trim());
aux++;
}

// InternetAddress[] address = { new InternetAddress(to) };

msg.setRecipients(Message.RecipientType.TO, (address));
msg.setSubject(subject);

// Teste
msg.setHeader("Disposition-Notification-To", from);
msg.setHeader("Return-Receipt-To", from);
msg.setHeader("X-Confirm-Reading-To", from);

// cria a primeira parte da mensagem
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(msgText1, "text/html");

// cria a segunda parte da mensage
MimeBodyPart mbp2 = new MimeBodyPart();

// anexa o arquivo na mensagem
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

// cria a Multipart
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

// adiciona a Multipart na mensagem
msg.setContent(mp);

// configura a data: cabecalho
msg.setSentDate(new Date());

// envia a mensagem
Transport.send(msg);

return retorno;

}





Em 23 de julho de 2014 14:06, Felipe Aron <felipearon@gmail.com> escreveu:
Fala galera, 

Estou tentando anexar um arquivo TXT usando o Javamail, porém o conteúdo do arquivo está sendo mostrado diretamente no corpo do email e não como arquivo anexado.

Meu código para anexar os arquivos:

        public void addAttachment(String filename) throws Exception {
DataSource file = new FileDataSource(filename);

BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(file)); 
attachment.setFileName(filename);
   multipart.addBodyPart(attachment);
}

Como fazer para anexar o arquivo TXT e não incorporar o conteúdo no corpo do e-mail?

--
Programador

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Henrique Nogueira de Melo
Analista e Arquiteto de Software

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Programador

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário