Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 27th, 2007, 06:52 AM
Registered User
Join Date: Feb 2007
Location: , , .
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?
Reply With Quote
  #2 (permalink)  
Old May 1st, 2007, 04:35 AM
Friend of Wrox
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post


Try the following code
     * 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;
        //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);

        // 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);

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

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

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();

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

        // Send the message
    }catch(Exception e){

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 06:24 AM.

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