Tem uma solução mais simples, abaixo:
Obs.: from, to, subject, etc são variáveis de
objeto da classe que possui o método abaixo
public void sendMail()
{
Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); Session session = Session.getDefaultInstance(props, null); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); msg.setSentDate(new Date()); MimeBodyPart mbp = new MimeBodyPart(); if ( text == null ) text = ""; mbp.setText(text); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp); /* Arquivos atachados */ File file; DataSource source; MimeBodyPart mbpFile; for (int i = 0; i < attachFiles.size(); i++) { file = new File((String) attachFiles.get(i)); if ( file.exists() ) { mbpFile = new MimeBodyPart(); source = new FileDataSource(file.getAbsolutePath()); mbpFile.setDataHandler(new DataHandler(source)); mbpFile.setFileName(file.getName()); mp.addBodyPart(mbpFile); } } msg.setContent(mp); Transport.send(msg); } catch ( MessagingException e ) { e.printStackTrace(); } }
|
- [java-list] Anexar arquivo em email. Alipio Krohn
- RE: [java-list] Anexar arquivo em email. Celeguim, Luiz (Cadmus)
- Eduardo Ribeiro da Silva