Pessoal, preciso de uma ajuda com envio de email.
É o seguinte, tenho um formulário com 03 campos tipo “file” e qdo eu coloco anexos em todos os campos o email é enviado normalmente. O meu problema é que qdo eu não escolho os 03 anexos nos campos ou seja quero enviar somente 02 anexos ou somente quero mandar a mensagem o email não é enviado.
Alguém pode me ajudar?
esse código achei na net e estou usando:
Properties props = new Properties();
props.put("mail.smtp.host", "mailhost.xxx.xxx.com");
Session s = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(s);
try {
InternetAddress from = new InternetAddress("[email removido]");
message.setFrom(from);
InternetAddress to = new InternetAddress("[email removido]");
//InternetAddress Bcc = new InternetAddress("[email removido]");
message.addRecipient(Message.RecipientType.TO, to);
//message.addRecipient(Message.RecipientType.BCC, Bcc);
message.setSubject("TDP OnLine");
} catch (Exception e) {
}
// cria a mensagem HTML a ser enviada
String mensagem = "<html><body>"+
"<table width=100% align=center border=0>"+
"<tr><td align=center><b>TDP OnLineº " +
"</td></tr></table>"+
"<br><br>"+
"<table width=100% align=center border=1>"+
"<tr><td align=center>TESTE</td></tr>"+
"</table></body></html>";
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = null;
multipart = new MimeMultipart();
try {
messageBodyPart.setContent(mensagem, "text/html");
multipart.addBodyPart(messageBodyPart);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int quantAnexos;
String caminhoAnexo[] = request.getParameterValues("file");
quantAnexos = caminhoAnexo.length;
try {
for (int i = 0; i < quantAnexos; i++) {
File file = new File(caminhoAnexo[i]);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Send the message
try {
Transport.send(message);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<p align='center'>E-Mail enviado com sucesso!</p>");