PDA

View Full Version : java mail


texerasmo
24-02-2006, 16:22
L'email me la invia anche con l'allegato ma ho un pb
Non riesco a far interpretare il messaggio in html.



public String sendAllegato(String host, String from, String to, String subject, String mes, String mimetype,String filename) {
String errore="";
try{

File f=new File(filename);

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);

// Create the multi-part
Multipart multipart = new MimeMultipart();

// Create part one
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(mes);

// Add the first part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(f);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);

// Add the second part
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send message
Transport.send(message);
System.out.println("Finito");


}catch(Exception e) {
errore=e.getMessage();
}
return errore;
}

pinok
24-02-2006, 18:35
Se intendi che non riesci a formattare in HTML il testo, prova a vedere se qua c'č qualcosa che ti puņ servire (l'ho estratto dal codice che uso io, ora analizzare dove inserirlo nel tuo mi viene lunga):

MimeBodyPart mainBody = new MimeBodyPart();
mainBody.setContent(testoMessaggio, "text/html");