Results 1 to 4 of 4
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Question javax.mail Attachements

    Well i am making an application with javax.mail api to send an email with attachement now the problem is this that when i send the mail it sends 2 copies of it , can anyone rectify and give me a reason why this is happening

    my code for sending the mail is
    Java Code:
    package sendmailattachment;
    
    import java.security.Security;
    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.swing.JOptionPane;
    
    public class sending
    {
        private static final String SMTP_HOST_NAME = "smtp.gmail.com";
        private static final String SMTP_PORT = "465";
        private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        String MsgTxt=null;
        String Subject=null;
        String From=null;
        String pwd=null;
        String[] too;
        String[] sss;
        
        public sending(String fr,String msg,String sub,String[] fro,String p,String[] ss)
        {
            MsgTxt=msg;
            Subject=sub;
            From=fr;
            pwd=p;
            too=fro;
            sss=ss;
            send();
        }
    
        public String send()
        {
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                try
                {
                    sendSSLMessage(too,Subject,MsgTxt,From,pwd,sss);
                }catch(Exception e)
                {
                    System.out.println(e);
                }
                return("Sucessfully Sent mail to User(s)");
        }
    
    
    
        public void sendSSLMessage(String[] recipients, String subject,String message, String from,String pwd,String[] ssss) throws MessagingException
        {
            boolean debug = true;
            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");
            final String from1=from;
            final String pwd1=pwd;
            Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
            {protected PasswordAuthentication getPasswordAuthentication()
                {return new PasswordAuthentication(from1,pwd1);}});
    
            session.setDebug(debug);
    
            MimeMessage msg = new MimeMessage(session);
            InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);
    
            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; i <1; i++)
            {
                addressTo[i] = new InternetAddress(recipients[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
    
            msg.setSubject(subject);
            
            MimeBodyPart mbp1 = new MimeBodyPart (  ) ;
            mbp1.setText ( message ) ;
            MimeBodyPart attachment[]=new MimeBodyPart[sss.length];
            for(int i=0;i<sss.length;i++)
            {
                attachment[i] = new MimeBodyPart() ;
                FileDataSource fds = new FileDataSource ( sss[i] ) ;
                attachment[i].setDataHandler ( new DataHandler ( fds )  ) ;
                attachment[i].setFileName ( fds.getName() ) ;
            }
            
            Multipart mp = new MimeMultipart (  ) ;
            mp.addBodyPart ( mbp1 ) ;
            for(int j=0;j<sss.length;j++)
            {
                mp.addBodyPart ( attachment[j] ) ;
            }
               //msg.setContent(message, "text/plain");
            msg.setContent(mp);
    
            Transport.send(msg);
            from=null;
            recipients=null;
            pwd=null;
            subject=null;
            message=null;
    
        }
    }
    sss[] contains list of file paths that r to be attached
    Last edited by ravjot28; 02-03-2010 at 10:14 PM.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    I've tried and no problems at all.

    I used mine gmail-account (and had to provide password because of auth,
    so it cannot be null).

    maybe you should post your main method
    so we can see your args.

    One more thing :

    Java Code:
     InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; [COLOR="Red"]i <1[/COLOR]; i++)
            {
                addressTo[i] = new InternetAddress(recipients[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
    is not good!

    Change it to : recipients.length

    TIP:
    For more efficient code always calculate array length (recipients.length) and put it in some local variable, then put variable in loop; this way calucation of that lenth is performed only once so you may use this:

    Java Code:
    int numberOfRec = recipients.length;
    		
    		InternetAddress[] addressTo = new InternetAddress[numberOfRec];
    		for (int i = 0; i < numberOfRec; i++)
    		{
    			addressTo[i] = new InternetAddress(recipients[i]);
    		}
    		msg.setRecipients(Message.RecipientType.TO, addressTo);

    I sent 1 message with 2 attached files
    on addresses of 2 recipients, no double messages.

  3. #3
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    Thanks for the reply see in this i dont have any problem when i send a message

    the problem is when i attach a file and then send . Then it sends 2 copies one copy containing the text message + attachment and the second copy contains only attachment this is my problem

    Hope you are understanding my query now

    Thanks in advance

  4. #4
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    and yes i have used array length func that was an error in posting sory for that

Similar Threads

  1. javax.mail.SendFailedException
    By anthrax in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 03:53 PM
  2. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 06:37 AM
  3. javax.mail.Transport
    By sharpi03 in forum Advanced Java
    Replies: 5
    Last Post: 10-23-2009, 06:26 PM
  4. Replies: 2
    Last Post: 05-05-2008, 07:01 AM
  5. I can't import javax.mail
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-04-2007, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •