bonjour,
Bon je veux envoyer un fichier joint , j’ai la classe java qui permet de le faire. Dans ma page hmtl je veux avoir soit un lien soit un bouton envoyer qui me permettra d'envoyer ce ficher tout en utilisant la classe java que j'ai.
voici la classe java :
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailAttachmentDemo {
public static void main(String[] args) {
EmailAttachmentDemo demo = new EmailAttachmentDemo();
demo.sendEmail();
}
public void sendEmail() {
String smtpHost = "";
String from = "";
String to = "";
String username = "";
String password = "";
String subject="";
String bodyText="";
String filename = "tt.txt";
Properties props = new Properties();
props.put("mail.smtp.host", "");
props.put("mail.smtps.port", "25");
props.put("mail.smtp.auth", "false");
try{
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
// Set the email message text.
//
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(bodyText);
//
// Set the email attachment file
//
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Partager