Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email attachment pro

Status
Not open for further replies.

Khanjan

Programmer
Feb 24, 2004
41
NL
Hi,

I am trying to send mail with attachment, but i cant do this.

Below is my code, Can someone tell me what is wrong with it??

simple.jsp:
<%
Properties props = new Properties();

props.put("mail.smtp.host", "smtp.quicknet.nl");
Session s = Session.getInstance(props,null);

MimeMessage message = new MimeMessage(s);

InternetAddress from = new InternetAddress("you@example.com");
message.setFrom(from);
String toAddress = request.getParameter("to");
InternetAddress to = new InternetAddress(toAddress);
message.addRecipient(Message.RecipientType.TO, to);

String subject = request.getParameter("subject");
message.setSubject(subject);
String text = request.getParameter("text");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText("Pardon Ideas");

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

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);

email.html

<html>
<head><title>S</title></head>
<body>
<p align="center"><b>A custom email utility.</b></p>
<form action="simple.jsp" method="post" ENCTYPE="multipart/form-data" >
<table align="center">
<tr><td>To</td><td><input name="to" size="50" /></td></tr>
<tr><td>Subject</td><td>
<input name="subject" size="50" value="[JavaMail Example - ]"/>
</td></tr>
<tr><td colspan="2">
<textarea name="text" cols="50" rows="20">Hello from JavaMail!</textarea>
<INPUT TYPE="file" NAME="thefile">
</td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send Email"/></td></tr>
</table>
</form>
</body>
</html>

Please help
 
Hi,

Where are u reading the file, that need to be attached ?

Cheers
Venu
 
Hi,

Well, i got this code from Java mail , i dont know how it works. There they didn't say anything about reading the file. though i had my doubts about it.


Can someone tell me how to do this, in jsp???

I have found a code in java , but not sure how to use it in combine with jsp and html.

public void attach(File file) {
try {
mbp = new MimeBodyPart();
mbp.setFileName(file.getName());
mbp.setDataHandler(new DataHandler(new FileDataSource(file)));
mp.addBodyPart(mbp);
} catch (MessagingException me) {
me.printStackTrace();
}
}

What should i add in my code to be able to send mail with attachments in JSP and How?

Any code or example will be appreciated, thanx
 
venur : The JavaMail API does not require you to explicitly read in a file - this is what the line below does :

mbp.setDataHandler(new DataHandler(new FileDataSource(file)));

Khanjan :

I have no idea why your program will not work - the JavaMail code is fine (I've just tested it)

I expect either :

- your parameters from the request are no good
- the smpt host doesn't exist.
- the file doesn't exist

If you don't understand how some code works, I would advise learning how it works - copying code from the internet is fine, as long as you understand it. Else, how will you ever solve your own problems ? You will have to constantly rely on others ...

PS, this is the test prgram I ran

Code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.*;

public class TestEmail {
	public static void main(String args[]) throws Exception {
	  Properties props = new Properties();

	  props.put("mail.smtp.host", "servername");
	  Session s = Session.getInstance(props,null);

	  MimeMessage message = new MimeMessage(s);

	  InternetAddress from = new InternetAddress("you@example.com");
	  message.setFrom(from);
	  String toAddress = "someone@somewhere.co.uk";
	  InternetAddress to = new InternetAddress(toAddress);
	  message.addRecipient(Message.RecipientType.TO, to);

	  String subject = "subject";
	  message.setSubject(subject);
	  String text = "text";

		// Create the message part
		BodyPart messageBodyPart = new MimeBodyPart();

		// Fill the message
		messageBodyPart.setText("Pardon Ideas");

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

		// Part two is attachment
		messageBodyPart = new MimeBodyPart();
		String filename = "C:/java/TestEmail.java";
		DataSource source = new FileDataSource(filename);
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName(filename);
		multipart.addBodyPart(messageBodyPart);

		// Put parts in message
		message.setContent(multipart);

		// Send the message
		Transport.send(message);
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top