Modifier la taille de la pièce jointe envoie mail avec javamail

chhibiJAVA Messages postés 30 Date d'inscription lundi 21 novembre 2011 Statut Membre Dernière intervention 26 septembre 2012 - 16 févr. 2012 à 22:15
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 17 févr. 2012 à 08:06
bonjour a tous,

mon code permet d'envoyer un mail avec une pièce jointe avec api javamail.
es qu’il est possible de modifier ou régler la taille d'une pièce jointe ?la taille de la pièce jointe ne doit pas etre supérieur a 100ko.

voici mon code:


import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
//import javax.mail.Address;
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 {

private String from, to, bodyText, subject;
private String smtp, filename;
private int port;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getFrom() {
return from;
}

public String getBodyText() {
return bodyText;
}

public void setBodyText(String bodyText) {
this.bodyText = bodyText;
}

public int getPort() {
return port;
}

public String getSmtp() {
return smtp;
}

public String getSubject() {
return subject;
}

public String getTo() {
return to;
}

public void setFrom(String from) {
this.from = from;
}

public void setPort(int port) {
this.port = port;
}

public void setSmtp(String smtp) {
this.smtp = smtp;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setTo(String to) {
this.to = to;
}

public EmailAttachmentDemo(String from, String to, String bodyText, String subject, String filename) {
this.from = from;
this.to = to;
this.bodyText = bodyText;
this.subject = subject;
this.filename = filename;
this.smtp = "smtp.topnet.tn";
this.port = 25;
}

public boolean validmail(String email) {
Pattern p = Pattern.compile("^[a-zA-Z]+[.]+[a-zA-Z]+@[a-zA-Z.]{2,}[.][a-zA-Z]{2,4}$");
Matcher m = p.matcher(email);
if (m.matches() == true) {
return true;
} else {
System.out.println("email non valider");
return false;
}
}

public boolean sendEmail() {
if (!validmail(getFrom())) {
return false;
}
if (!validmail(getTo())) {
return false;
}

Properties properties = new Properties();
properties.put("mail.smtp.host", getSmtp());
properties.put("mail.smtp.port", getPort());

Session session = Session.getDefaultInstance(properties, null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(getFrom()));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(getTo()));


message.setSubject(getSubject());
message.setSentDate(new Date());


MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(getBodyText());

MimeBodyPart attachmentPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource(getFilename()) {

@Override
public String getContentType() {
return "application/octet-stream";
}
};

attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(getFilename());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);


Transport.send(message);

System.out.println("mail envoyée");
return true;
} catch (MessagingException e) {
System.out.println("mail non envoyée");
// e.printStackTrace();
return false;

}

}

public static void main(String[] args) {
String from = "Chhibi.Ahmed@yahoo.fr";
String des = "chhibi.ahmed@gmail.com";
String msg = "bonjour";
String subject = "mailattachment";
String filename = "test.xlsx";

EmailAttachmentDemo demo = new EmailAttachmentDemo(from, des, msg, subject, filename);
if (!demo.sendEmail()) {
System.out.println("email non envoyée");
}
}
}

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
17 févr. 2012 à 08:06
Bonjour,

Je ne sais pas si tu peux modifier la taille avec javamail, mais en tout cas, tu peux toujours le faire avec java en utilisant JAI : http://java.sun.com/javase/technologies/desktop/media/jai/
0
Rejoignez-nous