Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 27th, 2007, 06:52 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default sending mails with attachments in smtp


I am using smtp for sending mails. Its working.
But i need to send mails with attachments. can any one of you send me the code for that?
 
Old May 1st, 2007, 04:35 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

Try the following code
[i]
    /**
     * This method sends the mail after authentication
     * @param subject subject of the mail
     * @param message massege to send in the mail
     * @param attachments mail attachements
     * @param from from address in the mail
     */
    public void sendMail( String subject, String message,String attachments[], String from){
// boolean debug = false;
        try{
        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        // Get session
        Session session = Session.getDefaultInstance(props,auth);
        //Session session = Session.getDefaultInstance(props,null);
        session.setDebug(true);

        // Define message
        MimeMessage messageObj = new MimeMessage(session);

        // Set From and To addresses
        messageObj.setFrom(new InternetAddress(from));

        /*InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        messageObj.setRecipients(Message.RecipientType.TO, addressTo);
        messageObj.setSubject(subject);
        */

        InternetAddress[] addressTo = new InternetAddress[1];
        addressTo[0] = new InternetAddress(To);
        messageObj.setRecipients(Message.RecipientType.TO, addressTo);
        messageObj.setSubject(subject);

        // create the message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        //fill message
        messageBodyPart.setText(message);

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

        //Attachments
        messageBodyPart = new MimeBodyPart();

        for (int i = 0; i < attachments.length; i++) {
            DataSource source = new FileDataSource(attachments[i]);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(attachments);
            multipart.addBodyPart(messageBodyPart);
        }
        // Put parts in message
        messageObj.setContent(multipart);

        // Send the message
        Transport.send(messageObj);
    }catch(Exception e){
        e.printStackTrace();
    }
}

Regards,
Rakesh





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to send attachments through a SMTP server Pawan Sangal Perl 3 December 7th, 2006 10:37 AM
How do I filter outgoing SMTP mails giftcalledlife@gmail.com Forum and Wrox.com Feedback 0 August 21st, 2006 06:30 PM
sending mails arabuafef Classic ASP Basics 0 July 18th, 2006 06:29 AM
Sending mails. rupen Javascript How-To 1 October 25th, 2005 07:22 AM
Sending mails rajeshnerenki Pro VB 6 4 September 25th, 2003 03:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.