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.


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

Results 1 to 2 of 2

Thread: Unable to send a mail with attachment

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Unhappy Unable to send a mail with attachment

    Hello Genius, here my problem was i am able to send a text mail simply in jsp. But With a attachment, I am not able to send mail through jsp coding.
    Here My coding. Please Help to solve this Problem want to work in jsp.

    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

    <%@page import="javax.mail.*" %>
    <%@page import="java.io.*" %>
    <%@page import="javax.activation.*" %>
    <%@page import="java.security.Security" %>
    <%@page import="java.util.Properties" %>
    <%@page import="javax.mail.Message" %>
    <%@page import="javax.mail.Session" %>
    <%@page import="javax.mail.Transport" %>
    <%@page import="javax.mail.internet.InternetAddress"%>
    <%@page import="javax.mail.internet.MimeMessage"%>
    <%@page import="javax.mail.internet.MimeBodyPart"%>
    <%@page import="javax.mail.internet.MimeMessage"%>
    <%@page import="javax.mail.internet.MimeMultipart"%>
    <%@page import="java.io.InputStream"%>
    <%@page import="java.util.Properties"%>

    <%@page import="java.util.Date"%>
    <%@page import="java.util.Properties"%>
    <%@page import="javax.activation.DataHandler"%>

    <%@ page import="java.util.Random" %>


    String email=request.getParameter("mailaddress");

    String username="zzz@gmail.com"; // Mailid
    String password="xxx"; //Password
    String recipients=email;

    String attachFiles[]=new String[50];
    String mailhost ="smtp.gmail.com";
    String subject="WWW.Uours.com";
    String filename=request.getParameter("f1"); // getting filename from previous page & assigned
    String mytext="Sample";
    String sender=username;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "111");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.n et.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");
    javax.mail.Authenticator pa;

    pa = new javax.mail.Authenticator()
    public javax.mail.PasswordAuthentication getPasswordAuthentication()
    return new javax.mail.PasswordAuthentication("Your MailID@gmail.com","Your Password");


    Session session1 = Session.getInstance(props,pa);

    Message message = new MimeMessage(session1);
    message.setFrom(new InternetAddress(sender));

    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(mytext, "text/html");

    // creates multi-part
    Multipart multipart = new MimeMultipart();

    MimeBodyPart attachPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    attachPart.setDataHandler(new DataHandler(source));
    attachPart.setFileName(new File(filename).getName());


    // sets the multi-part as e-mail's content

    // sends the e-mail

    //if (recipients.indexOf(',') > 0)
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));


    catch (MessagingException mex) {



  2. #2
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Default Re: Unable to send a mail with attachment

    Session session1 = Session.getInstance(properties,new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user,password);

Similar Threads

  1. Mail Send Module Not Working
    By hashitagarwal in forum Java SE APIs
    Replies: 1
    Last Post: September 14th, 2012, 10:25 AM
  2. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: February 26th, 2012, 04:21 PM
  3. Send Scanned File to E-mail
    By babywiz21 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2011, 08:40 PM
  4. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: November 14th, 2010, 11:00 AM
  5. Javamail attachment filenames
    By johniem in forum Java Theory & Questions
    Replies: 0
    Last Post: February 3rd, 2010, 07:51 AM