Upload d'une image et envoi directement par e-mail
Bonjour,
je débute en JavaEE, et je me heurte à un soucis de début...je m'explique:
j'ai créer un formulaire qui requiere une adresse mail et qui demande d'uploader une image.
Je verifie bien que l''e-mail est bien valide ainsi que l'image.
Toutefois, je n'arrive pas à faire passer en parametre les données de l'image dans ma classe sendmail.
voici mon code pour etre un peut plus clair.
upload.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.swing.text.html.parser.Parser;
import javax.mail.MessagingException;
@WebServlet("/Upload")
@MultipartConfig(
fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
maxFileSize = 1024 * 1024 * 10, // 10 MB
maxRequestSize = 1024 * 1024 * 15, // 15 MB
location = "C:\\tmp"
)
public class Upload<HttpServletRequest, HttpServletResponse> extends HttpServlet
{
private static final long serialVersionUID = 1L;
public static final String EMAIL = "email";
public static final String PICTURE = "imageClient";
public static final String VUE= "/WEB-INF/upload.jsp";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
}
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
System.out.println("[+] doPost");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String email = request.getParameter(EMAIL);
Part image = request.getPart(PICTURE);
try
{
if ((validationEmail(email) != false) && (isFileNameValid(image) != false))
{
out.write("<h2>[+] Send Email</h2></br>");
SendEmail send_notify = new SendEmail();
send_notify.sendnotification(email);
}
else
{
out.write("<h2>Fuck off !!!</h2>");
}
}
catch (Exception e)
{
System.out.println("[-] Erreur: " + e);
}
}
private boolean validationEmail(String email)
{
if ( email != null && email.trim().length() != 0 )
{
if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) )
{
System.out.println("[-] bad mail :(");
return false;
}
else
{
System.out.println("[+]good mail :)");
return true;
}
}
else
{
System.out.println("[-]empty mail :(");
return false;
}
}
private boolean isFileNameValid(final Parser part)
{
@SuppressWarnings("unused")
final String partHeader = part.getHeader("content-disposition");
String fileName="";
for (String content : part.getHeader("content-disposition").split(";"))
{
if (content.trim().startsWith("filename"))
{
fileName=content.substring( content.indexOf('=') + 1).trim().replace("\"", "");
}
}
if(fileName.contains(".jpg")
|| fileName.contains(".jpeg")
|| fileName.contains(".png")
|| fileName.contains(".gif")
|| fileName.contains(".bmp")
)
{
System.out.println("[+] good image :D");
return true;
}
else
{
System.out.println("[-] bad image :'(");
return false;
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class SendEmail {
public void sendnotification (String emailVisiteur) throws Exception
{
System.out.println("[+]email: " + emailVisiteur);
String MAIL_FROM = "admin@exxxx.com";
String MAIL_HOST ="localhost";
String MAIL_DEBUG = "false";
final Properties props = new Properties();
props.put("mail.smtp.host", MAIL_HOST);
props.put("mail.debug", MAIL_DEBUG);
final Session session = Session.getInstance(props);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(MAIL_FROM));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailVisiteur));
msg.setSubject("test");
String corps_message = "<html><head><title>Java Mail</title></head>" + "<body>Hello,<br/> World</b> </body></html>";
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent (corps_message, "text/html; charset=ISO-8859-1");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "C:\\......";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
msg.setContent(multipart);
// Send message
Transport.send(msg);
System.out.println("[+] Done !");
}
catch (MessagingException mex)
{
System.out.println("[-] Error:" + mex);
}
}
} |
note, je ne souhaite pas stocker l'image sur l'espace disque pour ensuite l'envoyer. je souhaite directement envoyé l'image par e-mail.
Si quelqu"un peut m'éclairer, je suis preneur.
Cordialement,