Prova questo codice:
codice:public void send(String filename){ Properties props=new Properties(); props.put("mail.smtp.host",serverSMTP); Session session = Session.getDefaultInstance(props, null); try { Message msg=new MimeMessage(session); msg.setFrom(new InternetAddress(emailFrom)); InternetAddress[] address = {new InternetAddress(emailTo)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); Transport transport=session.getTransport(address[0]); transport.connect(); //crea la prima parte MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(body); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(filename); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); transport.sendMessage(msg,address); }catch (Exception ex) { System.out.println("errore " +ex.getMessage());} }

Rispondi quotando