[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Email from website form w/ attachment using JavaMail API and nothing else

Asked by rleblanc41 in Java Server Pages (JSP), JavaMail, Java Servlets

Tags: JavaMail, Java, JSP

I am trying to send an e-mail with an attachment from a web site using ONLY the JavaMail API.  A user will be able to access a form (shown as FORM.JSP in the attached code) and can send across a subject, to, from, and CC.  Then they browse for their file and attach.  Upon clicking submit, the processMail.jsp page needs to build the e-mail out, attach the file, and send.

I have attached where I am so far in the code (thanks to rrz@871311).  I can't figure out how to extract the field information for subject, to, from, etc.  And the attachment comes across not as an attached file, but as gibberish in the e-mail body (see below).  I have also attached the test file I am using to attach in the test.

Please help!!!

This is what comes across in the e-mail:
 OLoOO?ALOOWCO OOO!#MWOAWOLOB?M?OOOOLoO#OOOOOOOOL_OOOOx$OOOOOOOOOM OOOOOOOOOOWOO KOMDOOI GO-O@OONOOOOOO1OOOOOOOB?MWON?OO!OOOOOO#OOOODOOOOM?OOOOOOOOO#OOOODOOOOM?OOOOOOOOL OI 9h )O DOH3OOOOOOOOOOOOOOOOOOH3OOOOOOAOOOOOAOOOOOH3OOOOOOOOOOOOOOOOL S?OOOOOOO   OOOOOOOOO  OOOOOOOOOOOOO@  OOOOOOOO  OOOOOOOOOOOOO@  OOOOOOG/OO5}OOOOOOOO}OOOOOOOOOOOOoOOO)OOOOOOOOIOOOOOOOOOOOO@  OOOOO9O@                OOOO@  
and on and on and on and on...
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:
FORM.JSP
-------------------------------------------------------------------------------------
<form name="form" action="processMail.jsp" method="post" enctype="multipart/form-data">
	<strong>From:</strong> <input name="from"><br>
	<strong>To:</strong> <input name="to"><br>
	<strong>Cc:</strong> <input name="cc"><br>
	<strong>Subject:</strong> <input name="subject"><br>
	<textarea name="body"></textarea><br>
	Upload your file: <input type="file" name="attachment" id="attachment" size="30" /><br>
	<input name="submit" type="submit" value="Submit Test" class="button" />
</form>
 
-------------------------------------------------------------------------------------
PROCESSMAIL.JSP
-------------------------------------------------------------------------------------
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.DataSource" %>
<%@ page import="javax.activation.DataHandler" %>
<%!
public class PostDataSource implements DataSource
{
        private ServletRequest req = null;
        private InputStream is = null;
        public PostDataSource(){super();}
        public PostDataSource( ServletRequest request ) throws IOException
        {
                req=request;
                is=req.getInputStream();
        }
        public String getContentType(){return req.getContentType();}
        public InputStream getInputStream(){return is;}
        public String getName(){return "post_data";}
        public OutputStream getOutputStream(){ return null;}
}
%>
<%
if (request.getContentType() != null)
{
        if (request.getContentType().toLowerCase().startsWith("multipart/form-data"))
        {
                ByteArrayInputStream bais = null;
                PostDataSource ds = new PostDataSource(request);
                MimeMultipart mp = new MimeMultipart(ds);
                int count = mp.getCount();
                out.print("Partcount=" + count + "<br/>");
                for (int i = 0; i < count; i++)
                {
                        MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(i);
                        String[] dis = bp.getHeader("Content-Disposition");
                        out.print(dis[0] + "<br/>");
                        out.print("Filename is " + bp.getFileName() + "<br/>");
                        String[] typ = bp.getHeader("Content-Type");
                        if(typ!=null)out.print("Content-Type===" + typ[0] + "<br/>");
                        String contype = bp.getContentType();
                        if (contype!=null)
                        {
                                out.print("ContentType is " + contype + "<br/>");
                                Object o = bp.getContent();
                                out.print("bodypartcontent is " + o + "<br/>");
                                out.print("Class===" + o.getClass() + "<br/>");
                                if (o instanceof ByteArrayInputStream)
                                {
                                        bais = (ByteArrayInputStream)o;
                                        out.print("NumberofBytes" + bais.available() + "<br/>");
                                }
                        }
                }
                String host = "192.168.1.1";
                Properties props = new Properties();
                props.put("mail.smtp.host", host);
                Session s = Session.getInstance(props,null);
                MimeMessage message = new MimeMessage(s);
                message.setFrom(new InternetAddress("emailfrom@domain.com"));
                message.addRecipient(Message.RecipientType.TO, new InternetAddress("emailto@domain.com"));
                message.setSubject("Website Attachment Submitted");
                MimeBodyPart mbp1 = new MimeBodyPart();
                mbp1.setText("This is a message body.");
                MimeBodyPart mbp2 = new  MimeBodyPart(bais); 	    
                Multipart mp2 = new MimeMultipart();
                mp2.addBodyPart(mbp1);
                mp2.addBodyPart(mbp2);
                message.setContent(mp2);
                Transport.send(message);
                out.print("message sent");
        } else out.print("not multipart/form-data request");
} else out.print("contentType null");
%>
Attachments:
 
Test file used in attachment...
Test file used in attachment...
 
 
Related Solutions
Keywords: Email from website form w/ attachme…
 
Loading Advertisement...
 
[+][-]10/08/09 01:32 PM, ID: 25529761Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/08/09 02:45 PM, ID: 25530478Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 05:12 AM, ID: 25534130Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 11:10 AM, ID: 25537350Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 11:55 AM, ID: 25537772Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 12:01 PM, ID: 25537825Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 12:16 PM, ID: 25537958Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 12:23 PM, ID: 25538022Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 12:25 PM, ID: 25538035Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 12:27 PM, ID: 25538059Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 12:34 PM, ID: 25538118Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 12:41 PM, ID: 25538187Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 12:53 PM, ID: 25538283Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 12:59 PM, ID: 25538325Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 01:07 PM, ID: 25538388Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 01:15 PM, ID: 25538462Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 01:38 PM, ID: 25538667Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/09/09 02:19 PM, ID: 25538960Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/09 11:03 AM, ID: 25562490Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/09 12:25 PM, ID: 25563453Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/09 12:36 PM, ID: 25563582Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/09 05:18 PM, ID: 25566128Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/09 07:36 PM, ID: 25566812Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/09 08:02 PM, ID: 25566888Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/14/09 10:19 AM, ID: 25572773Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/14/09 12:05 PM, ID: 25573909Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/14/09 01:28 PM, ID: 25574795Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/14/09 09:31 PM, ID: 25577257Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/14/09 10:04 PM, ID: 25577386Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 02:44 AM, ID: 25578825Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 08:18 AM, ID: 25581434Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 09:10 AM, ID: 25582024Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 09:15 AM, ID: 25582081Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 09:16 AM, ID: 25582090Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 09:23 AM, ID: 25582161Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 09:34 AM, ID: 25582276Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 09:37 AM, ID: 25582295Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 09:47 AM, ID: 25582374Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 11:31 AM, ID: 25583369Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 12:18 PM, ID: 25583782Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 12:22 PM, ID: 25583814Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 01:53 PM, ID: 25584796Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 01:53 PM, ID: 25584802Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 04:05 PM, ID: 25585739Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/09 04:12 PM, ID: 25585777Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/16/09 05:50 PM, ID: 25594345Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/20/09 11:31 AM, ID: 25617024Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/20/09 06:56 PM, ID: 25620263Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/20/09 07:08 PM, ID: 25620333Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/21/09 04:08 AM, ID: 25622660Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/21/09 08:47 AM, ID: 25625162Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/23/09 07:44 AM, ID: 25644884Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/23/09 07:45 AM, ID: 25644902Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/24/09 11:17 PM, ID: 25655690Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/26/09 10:14 PM, ID: 25669298Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/09 09:07 AM, ID: 25674084Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625