Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to attach a file to the mail in jsp

843830Apr 3 2006 — edited Apr 4 2006
hi.i would like to send mails with in local server with files as attachments.i wrote this code but its not working.please help



<%@ page import="javax.mail.*,javax.mail.internet.*,java.util.Date,java.io.*,java.net.InetAddress,java.sql.*,java.util.Properties,java.net.*,javax.sql.*,javax.activation.*,java.util.*,java.text.*" %>

<%@ page import="java.io.*,java.sql.*,java.net.*,java.util.*,java.text.*" %>
<%
String Attachfiles1="";
if("Send".equalsIgnoreCase("send"))
{
try
{
String subject="",from="",url = null,to="";
String mailhost = "local server";
Properties props = System.getProperties();
String msg_txt="";
String strStatus="";
String mailer = "MyMailerProgram";
to=request.getParameter("to");
from=request.getParameter("from");
subject=request.getParameter("subject");
msg_txt=request.getParameter("message");
Attachfiles1=request.getParameter("fieldname1");

props.put("mail.smtp.host", mailhost);
Session mailsession = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(mailsession);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// message.setSubject(subject);
// message.setHeader("X-Mailer", mailer);
// message.setSentDate(new Date());
message.setText(msg_txt);



// Final message = part1 + part2
Multipart mp = new MimeMultipart();
message.setContent(mp);


// Message part 1 is text - add to multipart
MimeBodyPart m1 = new MimeBodyPart();
m1.setContent(msg_txt, "text/plain");
mp.addBodyPart(m1);

// Message part 2 is binary (file to send)
MimeBodyPart m2;

// Support multiple file send (separated by ";")

FileDataSource fds = new FileDataSource(Attachfiles1);
m2 = new MimeBodyPart();
m2.setDataHandler(new DataHandler(fds) );

m2.setFileName( new File(Attachfiles1).getName() );
mp.addBodyPart(m2); // add to final message (part 2)
//
//

//message.addAttachment(new Attachment(new File(Attachfiles1)));



//BodyPart messageBodyPart = new MimeBodyPart();
//BodyPart messageBodyPart2 = new MimeBodyPart();
//Multipart multipart = new MimeMultipart();
////messageBodyPart = new MimeBodyPart();
//javax.activation.DataSource source = new javax.activation.FileDataSource(Attachfiles1);
//messageBodyPart.setDataHandler(new DataHandler(source));
////messageBodyPart.setFileName(new File(Attachfiles1).getName());
//messageBodyPart2.setText(msg_txt); // set the txt message
//multipart.addBodyPart(messageBodyPart);
//multipart.addBodyPart(messageBodyPart2);
//message.setContent(this.multipart);

Transport.send(message);

out.println("Message Sent");
}
catch (Exception e)
{
e.printStackTrace();
}
}

%>
<html>
<body>

<div class="frame">
<form action="Composemailpp.jsp" method="post">
<b>SelectPosition:</b> <select name="cars" >
<option value="ABAP">ABAP
<option value="JAVA">JAVA
<option value="DOTNET">DOTNET
<option value="SAP">SAP
</select><br><br>
<table border="1" cellpadding="2" cellspacing="2">
<tr><th>
Name<br><input type="text" name="Name"><br>
</th>
<th>
EmailId<br><input type="text" name="EmailId"><br>
</th>
<th>
ContactNumber<br><input type="text" name="ContactNumber"><br>
</th>
<th>
Position<br><input type="text" name="Position"><br>
</th>
</tr>
</table><br>
<b>SelectUser :</b><select name="cars">
<option value="Administrator">Administrator
<option value="User1">User1
<option value="User2">User2
<option value="User3">User3
<option value="User3">User4
</select>
<br>
<b>To :</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="to" size="72"><br>
<b>From :</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="from" size="72"><br>
<b>Subject :</b>&nbsp&nbsp&nbsp<input type="text" name="subject" size="72"><br>

<b>Message:</b><br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<textarea rows="10" cols="50" name="message">
</textarea> <br><br>
<b>AttachedFile:</b>&nbsp<input type="file" name="fieldname1" value="filename" size="50"><br><br>
<!--<b>AttachedFile:</b>&nbsp<input type="file" name="fieldname2" value="filename" size="50"><br><br>
<b>AttachedFile:</b>&nbsp<input type="file" name="fieldname3" value="filename" size="50"><br><br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="submit" name="attachfiles" value="Attachfiles">
-->
<center>
<input type="submit" name="send" value="Send" >
</center>
</form>
</div>
</body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 2 2006
Added on Apr 3 2006
8 comments
537 views