Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JSP Email Attachments

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JSP Email Attachments

    Hello,

    I am not able to send email attachment with JSP, I was just trying as although the ethics says code should be separated from JSP.

    Here is my code below, remaining things like smtpuser, username and password are mentioned appropriately and plain email sending is working.

    host ="smtp.gmail.com"; //"smtp.gmail.com";

    user ="dpspne@gmail.com"; //"YourEmailId@gmail.com" // email id to send the emails

    pass ="xyzzzz"; //Your gmail password

    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    String to ="ryanpune@gmail.com"; // out going email id

    String from ="dpspne@gmail.com"; //Email id of the recipient

    String subject = request.getParameter("subject");
    String messageText = request.getParameter("message");
    String fileName = request.getParameter("thefile");
    boolean sessionDebug = true;
    String action = request.getParameter("action");
    if ("Send".equals(action)) {
    Properties props = System.getProperties();
    props.put("mail.host", host);
    props.put("mail.transport.protocol.", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(sessionDebug);
    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setContent(messageText, "text/html"); // use setText if you want to send text
    //Added for attachment
    //messageBodyPart.setText("Pardon Ideas");
    // Create the message part
    javax.mail.BodyPart messageBodyPart = new MimeBodyPart();
    // Fill the message
    messageBodyPart.setText(messageText);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    //MimeBodyPart attachmentPart = new MimeBodyPart();
    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    if ("Upload".equals(action)) {

    DataSource source = new FileDataSource(fileName);

    //messageBodyPart.setDataHandler(new DataHandler(source));

    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(fileName);
    multipart.addBodyPart(messageBodyPart);
    }
    // Put parts in message"
    msg.setContent(multipart);


    // end of mutipart
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);
    try {
    transport.sendMessage(msg, msg.getAllRecipients());

    out.println("message successfully sent"); // assume it was sent
    }
    catch (Exception err) {

    out.println("message not sent"); // assume it’s a fail
    }
    transport.close();
    }

    Could anyone please let me know why it is not working and am not able to send attachment.
    The HTML Form is as below

    <form>
    <table>
    <tr>
    <td align="center" colspan="2"><b>Pease send an email to let us know your requirement. Our representative will get back to you</b></td>
    </tr>
    <!-- <tr>
    <td align="right">From:</td>
    <td><input type="text" name="from"></td>
    </tr>
    <tr>
    <td align="right">To:</td>
    <td><input type="text" name="to"></td>
    </tr> -->
    <tr>
    <td align="right">Subject:</td>
    <td><input type="text" name="subject"></td>
    </tr>
    <tr>
    <td align="right">Message:</td>
    <td><textarea rows="15" cols="25" name="message"></textarea></td>
    </tr>


    <tr>
    <td align="right" colspan="2">
    <INPUT TYPE="file" NAME="thefile">
    <input type="submit" name="action" value="Upload">&nbsp;</td>
    </tr>
    <tr>
    <td align="right" colspan="2">
    <input type="submit" name="action" value="Send">&nbsp;<input type="reset"></td>
    </tr>
    </table>
    </form>


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Bangalore
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JSP Email Attachments

    I am also having same Problem in jsp. Please send me answer, if you had worked correctly. My mail-id ragavan.j89@gmail.com

  3. #3

    Default Re: JSP Email Attachments

    It's hard to help without any detailed exception/error faced by the original poster.
    However you can see a working example regarding send e-mail with attachment.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JSP Email Attachments

    Please don't resurrect year old posts. And further, please don't ask for answers via email - that defeats the purpose of public forums such as this

    Please create your own thread - reference this thread if needed. Thread locked.

Similar Threads

  1. SAAJ giving exception with large attachments
    By Veronica in forum Web Frameworks
    Replies: 2
    Last Post: January 4th, 2011, 04:13 AM
  2. Images not going in email
    By anjali09s in forum Java SE APIs
    Replies: 3
    Last Post: August 2nd, 2009, 06:06 PM