|
[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
|
||
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");
%>
|
Advertisement
| Hall of Fame |