Envoyer une pièce jointe avec java

saifesprit Messages postés 2 Date d'inscription vendredi 12 février 2010 Statut Membre Dernière intervention 30 décembre 2010 - 30 déc. 2010 à 02:03
saifesprit Messages postés 2 Date d'inscription vendredi 12 février 2010 Statut Membre Dernière intervention 30 décembre 2010 - 30 déc. 2010 à 21:16
Bonjour tous le monde.
Mon problème est d'envoyer un email avec une pièce jointe(fichier texte) a une adresse destination. Le code ci-joint me permet d'envoyr un email sans pièce jointe, j'ai essayé avec Message.attachement.add(fichier) mais ca marche pas.Svp si vous avez une idée aider moi.et merci d'avance.........
javascript:void(0);
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
*
* @author Administrateur
*/
public class test1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {


try
{
String smtpServer="smtp.topnet.tn";
String to="saifesprit@gmail.com";
String from="saifesprit@gmail.com";
String subject="félicitation c ton premier email envoyer a partir de votre application POST TRAITEMENT DES FRICHIERS LOG";


String body="test email";
send(smtpServer, to, from, subject, body);
}
catch (Exception ex)
{
System.out.println("Usage: java com.lotontech.mail.SimpleSender"
+" smtpServer toAddress fromAddress subjectText bodyText");
}
System.exit(0);
}
public static void send(String smtpServer, String to, String from
, String subject, String body)
{
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());



// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

2 réponses

lural Messages postés 131 Date d'inscription samedi 6 janvier 2007 Statut Membre Dernière intervention 4 janvier 2011 2
30 déc. 2010 à 11:16
Bonjour,

Un article très pratique : http://www.developer.com/java/other/article.php/618471

Sinon je lis un javascript:void(0) au début de ton code... C'est une erreur de copier coller ?
0
saifesprit Messages postés 2 Date d'inscription vendredi 12 février 2010 Statut Membre Dernière intervention 30 décembre 2010
30 déc. 2010 à 21:16
Merci pour votre réponse mais voilà ce que je cherche, c'est la partie manquante dans mon code.

String filename="chemin ver le fichier";


MimeBodyPart mbp2 = new MimeBodyPart();

// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp2);

// add the Multipart to the message
msg.setContent(mp);
0
Rejoignez-nous