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 11 of 11

Thread: Javax.Mail Query Help Me Please

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

    Default Javax.Mail Query Help Me Please

    Hi all

    I made a GUI based code to send mails using javax.mail api now i made my code in Net Beans and i just made a library of javax.mail and attached it to my project
    Everything is working fine using netbeans

    The problem arises when i create a jar file of it and try to run in other computer there the mail does not send

    The problem is that javax.mail api is not in that PC so how can i add javax.api in that api without manually adding javax.mail in the system path of PC

    This problem is also in my PC too even though i have added the path in system variable path but even then i cant run the jar file

    Help me please


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javax.Mail Query Help Me Please

    Hello, either you can shadow the mail.jar into your programs jar as in deflate it and pack the contents into your jar or you can simply make sure your classpath is correct in the manifest file and make sure you ship the mail.jar with your program.

    // Json

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javax.Mail Query Help Me Please

    Well thanks for ur reply can u give me a snippet so that i can understand what you are trying to express
    Please

    Thanks

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javax.Mail Query Help Me Please

    See Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

    At the bottom of that page you can see that you can specify a classpath entry in the manifest file.

    Class-Path: mail.jar
    Now all you need to do is have the mail.jar next to your own jar file and it should find the mail.jar file and put it on the classpath.

    // Json

  5. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javax.Mail Query Help Me Please

    hi i tried your method but when i execute the jar file it gives me an error
    "Cant find main class GUI"

    my manifest file is

    Class-Path: mail.jar
    Main-Class: GUI

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javax.Mail Query Help Me Please

    When you specify Main-Class: GUI it means that java will try to locate a class file GUI.class in the root of the jar file. This needs to be the fully qualified package/class name of the class containing the public static void main(String... arguments) method.

    // Json

  7. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javax.Mail Query Help Me Please

    well i am sending you my code you tell me what should be my manifest file

    GUI.java

    package sendmail;
    import sendmail.sending;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class GUI implements ActionListener
    {
        private Frame f;
        private Button b1,b2;
        private TextField from,to,pwd,sub;
        private TextArea body;
        private Label l1,l2,l3,l4,l5;
        private Panel p1,p2,p3,p4,p5,p6;
        public GUI()
        {
            f=new Frame("Send Mail");
            f.addWindowListener(new WindowAdapter()
            {      public void windowClosing(WindowEvent e) {        actionExit();      }    });
     
            from=new TextField(30);
            pwd=new TextField(30);
            pwd.setEchoChar('*');
            to=new TextField(30);
            sub=new TextField(30);
            body=new TextArea(30,100);
            b1=new Button("Send");
            b2=new Button("Reset");
            l1=new Label("From (ID) *");
            l2=new Label("Password *");
            l3=new Label("To *");
            l4=new Label("Sub *");
            l5=new Label("Body");
            p1=new Panel();
            p2=new Panel();
            p3=new Panel();
            p4=new Panel();
            p5=new Panel();
            p6=new Panel(new GridLayout(3,1));
        }
     
        public void launch() throws Exception
        {
            f.setLayout(new BorderLayout());
            p1.add(l1);
            p1.add(from);
            p1.add(l2);
            p1.add(pwd);
            p2.add(l3);
            p2.add(to);
            p3.add(l4);
            p3.add(sub);
            p4.add(body);
            p5.add(b1);
            p5.add(b2);
            p6.add(p1);
            p6.add(p2);
            p6.add(p3);
            f.add(p6,BorderLayout.NORTH);
            f.add(p4,BorderLayout.CENTER);
            f.add(p5,BorderLayout.SOUTH);
            b1.addActionListener(this);
            b2.addActionListener(this);
            f.pack();
            f.setVisible(true);
        }
        private void actionExit()
        {
            System.exit(0);
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getActionCommand().equals("Send"))
            {
               String fr=from.getText();
               String p=pwd.getText();
               String s=sub.getText();
               String b=body.getText();
               StringTokenizer st1=new StringTokenizer(to.getText(),",");
               String[] t=new String[st1.countTokens()];
               int i=0;
               while(st1.hasMoreTokens())
               {
                   t[i]=st1.nextToken();
                   i++;
               }
               System.out.println("Sending the data to sending class--->");
               sending s1=new sending(fr,b,s,t,p);
               String result=s1.send();
               body.setText("");
               body.setText(result);
            }
            else
            {
                from.setText("");
                pwd.setText("");
                to.setText("");
                sub.setText("");
                body.setText("");
            }
        }
     
        public static void main(String args[]) throws Exception
        {
            GUI a=new GUI();
            a.launch();
        }
     
     
    }

    sending.java

    package sendmail;
     
    import java.security.Security;
    import java.util.Properties;
     
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
     
    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;
     
        public sending(String fr,String msg,String sub,String[] fro,String p)
        {
            MsgTxt=msg;
            Subject=sub;
            From=fr;
            pwd=p;
            too=fro;
            for(int i=0;i<too.length;i++)
            {
                System.out.println(too[i]);
            }
     
        }
     
        public String send()
        {
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                try
                {
                    sendSSLMessage(too,Subject,MsgTxt,From,pwd);
                }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) 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);
     
            Message 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);
            msg.setContent(message, "text/plain");
            Transport.send(msg);
            from=null;
            recipients=null;
            pwd=null;
            subject=null;
            message=null;
     
        }
    }

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javax.Mail Query Help Me Please

    Try altering your main-class to this.

    Main-Class: sendmail.GUI
    // Json

  9. #9
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javax.Mail Query Help Me Please

    See i made this code in Netbeans and used the mail.jar as library and included to my project

    But now i just copied the .class files to desktop and copied mail.jar file to desktop

    and on the desktop i am making the manifest.mf

    So using sendmail.GUI will not help

  10. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javax.Mail Query Help Me Please

    But the GUI.class have to be in a sendmail folder because of the package declaration at the top in your code or it wont work anyways.

    Could you try attaching the files here and I will try to create a jar file for you when I have some more spare time.

    // Json

  11. #11
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Javax.Mail Query Help Me Please

    well thanks i forgot i had a package sorry to disturb you now it is functioning thanks anyways

Similar Threads

  1. Again Interesting JButton query
    By ravjot28 in forum AWT / Java Swing
    Replies: 0
    Last Post: January 14th, 2010, 11:22 AM
  2. [SOLVED] Interesing JButton Query
    By ravjot28 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 14th, 2010, 11:19 AM
  3. MSSQL Query Question
    By sravyen in forum JDBC & Databases
    Replies: 1
    Last Post: October 13th, 2009, 03:47 AM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 08:59 AM
  5. Sending and Receiving mail using J2ME without server
    By chals in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: June 2nd, 2009, 10:59 AM