Envoi mail avec pièce jointe avec SMTP

cs_franck09 Messages postés 2 Date d'inscription vendredi 15 septembre 2006 Statut Membre Dernière intervention 18 septembre 2006 - 18 sept. 2006 à 10:57
imanota Messages postés 16 Date d'inscription dimanche 21 septembre 2008 Statut Membre Dernière intervention 28 mai 2010 - 28 mai 2010 à 17:44
Bonjour,
Je voudrais savoir comment on attache une pièce jointe lorsque l'on envoie un mail à la main en utilisant le protocole SMTP. Je sais qu'il faut encoder en MIME mais quelqu'un pourrait m'expliquer merci.

4 réponses

cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
18 sept. 2006 à 12:24
0
cs_franck09 Messages postés 2 Date d'inscription vendredi 15 septembre 2006 Statut Membre Dernière intervention 18 septembre 2006
18 sept. 2006 à 14:06
Je ne recherche pas un code mais la ou les commandes utilisées par le protocole SMTP pour envoyer des mails avec une pièce jointe attachée. Je voudrais simplement avoir le détail pour attacher une pièce jointe.
(commande SMTP : helo, mail from:....) Je voudrais savoir celle pour attacher une pièce jointe merci.
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
18 sept. 2006 à 14:22
attend, ca se trouve en deux secondes avec Google
fait tes recherches!!!
Bob...
"La chance accorde ses faveur aux esprits avertis..."
0
imanota Messages postés 16 Date d'inscription dimanche 21 septembre 2008 Statut Membre Dernière intervention 28 mai 2010
28 mai 2010 à 17:44
slt,svp je veux envoyer un mail avec piece joint a partir d'un formulaire;j'ai voila le code que j'utilise,il ne me returne pas d'erreur ms ca marche pas,s'il vs plait qui peut m'aider;voila le code:


index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Java Mail </title>
</head>



<form action="sendMail.jsp" method="POST">
Send Mail ,
----

votre nom et prenom, :,
,

----

Subject, :,
,

----

Message, :,
<textarea name="message" rows="8" cols="30">
</textarea>,

----
article:,
----



</form>


</html>

sendMail.jsp:



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="mail" scope="session" class="esai.Mail" />
<jsp:setProperty name="mail" property="to" value="noursabah1@hotmail.fr" />
<jsp:setProperty name="mail" property="from" value="Java.Mail.CA@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="nom" param="nom"/>
<jsp:setProperty name="mail" property="filename" param="filename"/>
<jsp:setProperty name="mail" property="message" param="message" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Send Mail </title>
</head>


<%
String to =mail.getTo();
int result;

result = mail.sendMail();

if(result == 0){
out.println(" Mail Successfully Sent to "+to);
}
else{
out.println(" Mail NOT Sent to "+to);
}

%>



</html>


Mail.java:

package esai;



import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
//import java.util.logging.*;
import javax.activation.*;

public class Mail {
//Ajout de la partie pièce jointe
private String to;
private String from;
private String message;
private String subject;
private String smtpServ;
private String nom;
//DataSource source;
private String filename;

public String getfilename() {
return filename;
}
public void setfilename(String filename) {
this.filename = filename;
}


public String getnom() {
return to;
}

public void setnom(String nom) {
this.nom = nom;
}

public String getTo() {
return to;
}

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

public String getFrom() {
return from;
}

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

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message; }

public String getSubject() {
return subject;
}

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

public String getSmtpServ() {
return smtpServ;
}

public void setSmtpServ(String smtpServ) {
this.smtpServ = smtpServ;
}

public int sendMail(){
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);

Message msg = new MimeMessage(session);





BodyPart messageBodyPart = new MimeBodyPart();


msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
messageBodyPart.setText(message);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);


messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent to"+to+" OK.");
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}

private class SMTPAuthenticator extends javax.mail.Authenticator {


@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "imanota@gmail.com";
String password = "mohamadamine";
return new PasswordAuthentication(username, password);
}
}
}





imanota
0
Rejoignez-nous