Hi Louie,
I have written a bean to sent a attachment thorough
JSP.
U have to specify the host and to is the address to
whom u are going to sent.
U can view the code
CODE IS
import java.beans.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
//import javax.servlet.http.*;
/**
* sendfile will create a multipart message with the
second
* block of the message being the given file.<p>
*
* This demonstrates how to use the FileDataSource to
send
* a file via mail.<p>
*
*
* @author Balamurugan S Raman
*/
public class EmailBean {
String to = null;
String from = null;
String host = null;
String filename = null;
boolean debug = false;
String bodyText = null;
String subject = null;
boolean processError = false;
public EmailBean (){
to = new String ("[EMAIL PROTECTED]");
from = new String ("[EMAIL PROTECTED]");
host = new String ("207.65.27.2");
filename = new String ("c:\\anil.htm");
debug = false;
bodyText = new String ("Attn:file attached");
subject = new String ("File attached");
}
// set and get methods --- start
public void setTo (String adr) {
to = adr;
}
public String getTo () {
return to;
}
public String getFrom () {
return from;
}
public void setFrom (String adr) {
from = adr;
}
public String getHost () {
return host;
}
public void setHost (String adr) {
host = adr;
}
public void setFileName (String fileadr) {
filename = fileadr;
}
public String getFileName () {
return filename;
}
public void setDebug (boolean boolvalue) {
debug = boolvalue;
}
public boolean getDebug () {
return debug;
}
public void setSubjectText (String subjectTxt) {
subject = subjectTxt;
}
public String getSubjectText () {
return subject;
}
public void setBodyText (String bodyTxt) {
bodyText = bodyTxt;
}
public String getBodyText () {
return bodyText;
}
// set and get methods --- start
public void sendfile (){
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props,
null);
session.setDebug(debug);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new
InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,
address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(bodyText);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new
FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
--- Louie Tao <[EMAIL PROTECTED]> wrote:
> Hello,
>
> we can send regular email.
> However, we need to handle any kind of files (eg
> text file, jpg file etc)
> in the attachment if there is an attachment.
>
> Any ideas or a few lines of code will be
> appreciated
>
> thanks in advance
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with
> body: "signoff JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body:
> "set JSP-INTEREST DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets