Javamail and Finding File Existence

1) How to use the javamail concept in a jsp. I want to send just a simple mail thru this.

2) How to find the existence of a particular file in java. I want to display the images if it was found. If the
    image is not found then a consatnt image is displayed.

Pankaj

TO SEND A EMAIL USING FILE IN JSP

JSP
SimpleSender objSendMail = new SimpleSender();
try
    {
      bSent = objSendMail.send(smtpServer, to, from, subject, body, localfile, attachName);
    }
    catch (Exception ex)
    {
      System.out.println("Usage: SimpleSender smtpServer toAddress fromAddress subjectText bodyText");  }
/**

* A simple email sender class.
*/
public class SimpleSender
{

/**
* "send" method to send the message.
*/
public boolean send(String smtpServer, String to, String from, String subject, String body, String localfile, String attachName)
{
boolean bSent = false;
try
{
Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);

// -- Create a new message --
Message msg = new MimeMessage(session);

// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,
false));

// -- We could include CC recipients too --
// if (cc != null)

// msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc,
false));

// -- Set the subject and body text --
msg.setSubject(subject);

// Uncomment if body text to be sent
//msg.setText(body);

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

// Fill the message
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

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

// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());

// -- Send the message --
Transport.send(msg);
bSent = true;

//System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return bSent;
}
}

RimZim Sinha

To check if a file exist :

try{

RandomAccessFile acfp = new RandomAccessFile("xyz.txt", "r");

//display the image

}
catch(filenotfoundexception f){
//display the constant image
}

RimZim Sinha

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.