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

    Default Java Mail Api Problem

    Well i developed this code and try to send mail with attachments using Mail API. My problem is this when i send the mail its sends 2 copies of it i.e. 2 mails are sent with same content . Below is my code please help me

    Java Code:
    import java.security.Security;
    import java.util.Properties;
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class sendingMail
    {
        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;
        static int count=0;
        
        public sendingMail(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()
        {
            String s="";
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                try
                {
                    sendSSLMessage(too,Subject,MsgTxt,From,pwd,sss);
                }catch(Exception e)
                {
                    s=e.getMessage();
                }
                if(s.equals(""))
                {
                    s="Your message is successfully mailed";
                }
                return(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 <recipients.length; 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;
            
        }
    }
    Thank you in advance :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Have you tried debugging your code by adding println() statements to see where the code is executing?
    Where do you create the sendingMail object?

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

    Default

    well i made a simple GUI in that i call sendMail when send button is clicked

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Have you tried debugging your code by adding println() statements to see where the code is executing?
    Also if you can't search the source to find who is calling, add the following line where you want to know who is calling that method:
    Java Code:
    try{throw new Exception("Who called");}catch(Exception x) {x.printStackTrace();}

Similar Threads

  1. Java Mail Multipart Problem
    By lowell108 in forum Advanced Java
    Replies: 0
    Last Post: 01-04-2010, 02:36 PM
  2. a problem about java mail client program
    By lunarstyle in forum AWT / Swing
    Replies: 9
    Last Post: 12-19-2009, 04:14 PM
  3. problem with sending mail usin javaX.mail api
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 11-25-2009, 05:37 AM
  4. Problem in sending mail from java
    By npoorni in forum Advanced Java
    Replies: 1
    Last Post: 06-30-2009, 04:09 PM
  5. Java mail problem Urgent
    By sundarjothi in forum Advanced Java
    Replies: 0
    Last Post: 06-02-2008, 11:34 AM

Posting Permissions

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