Results 1 to 7 of 7
  1. #1
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Unhappy Please help me in javax.mail to send a email..

    please help me solve the problem regarding sending an email by using java...

    firstly, i have 2 java classes.
    for the first class, i create a GUI that enables the user to enter the required data such as Sender email, receiver email, and smtp server..for the 2nd class, i make it a class to enable my application to send an email.
    so, i will post my code.
    *********************
    //first class email.java
    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class email extends JFrame implements ActionListener
    {
    JFrame jfrm = new JFrame("Email Setting");
    JLabel file;
    JLabel smtp;
    JLabel port;
    JLabel sender;
    JLabel username;
    JLabel password;
    JLabel receiver;
    JLabel room;
    JTextField filetext;
    static JTextField smtptext;
    JTextField porttext;
    static JTextField sendertext;
    JTextField usernametext;
    JTextField passwordtext;
    static JTextField receivertext;
    JPanel panel;
    JPanel panel2;
    Container cpane;
    
    JButton Send;
    JButton Cancel;
    JButton Refresh;
    
    JMenuBar mb			 = new JMenuBar(); 
    JMenu mnuFile			 = new JMenu("File"); 
    JMenuItem mnuItemQuit	 = new JMenuItem("Quit");
    JMenu mnuHelp			 = new JMenu("Help");
    JMenuItem mnuItemAbout  = new JMenuItem("About");
    
    public email()
      {
    	setTitle("Email Setting");
            setBounds(456, 150, 450, 330);
            setVisible(true);
            setJMenuBar(mb);
    
            mnuFile.add(mnuItemQuit);
            mnuHelp.add(mnuItemAbout);
            mb.add(mnuFile); 
            mb.add(mnuHelp);
    
    	cpane = getContentPane();
            cpane.setLayout(new FlowLayout());
            file		 = new JLabel(" File ");
            smtp	         = new JLabel(" Smtp server ");
            port		 = new JLabel(" Port ");
            sender 	 = new JLabel(" Sender ");
            username 	 = new JLabel(" Username ");
            password     = new JLabel(" Password ");
            receiver	 = new JLabel(" Recepient");
    	filetext 	 = new JTextField(15);
            smtptext	 = new JTextField(15);
            porttext     = new JTextField(15);
            sendertext	 = new JTextField(15);
            usernametext = new JTextField(15);
            passwordtext = new JTextField(15);
            receivertext = new JTextField(15);
            Send 		 = new JButton("Send");
            Cancel		 = new JButton("Cancel");
            Refresh		 = new JButton("Refresh");
            Send.addActionListener(this);
            Cancel.addActionListener(this);
            Refresh.addActionListener(this);
            mnuItemQuit.addActionListener(this);
            mnuItemAbout.addActionListener(this);
    		panel = new JPanel(new GridLayout(8, 1, 5, 0));
    		panel.add(file);
    		panel.add(filetext);
    		panel.add(smtp);
    		panel.add(smtptext);
    		panel.add(port);
    		panel.add(porttext);
    		panel.add(sender);
    		panel.add(sendertext);
    		panel.add(username);
    		panel.add(usernametext);
    		panel.add(password);
    		panel.add(passwordtext);
    		panel.add(receiver);
    		panel.add(receivertext);
    		panel2 = new JPanel(new GridLayout(1, 5, 5, 0));
    		panel2.add(Send);
    		panel2.add(Cancel);
    		panel2.add(Refresh);
    
    		cpane.add(panel);
    		cpane.add(panel2, BorderLayout.SOUTH);
        }
        	public void actionPerformed (ActionEvent ae){
    
    		if(ae.getSource() == Send){
    			new emailprocess();
    		}
    		if(ae.getSource() == Cancel){
    
    			dispose();
    		}
    		if(ae.getSource() == Refresh){
    
    		}
    		if(ae.getSource() == mnuItemQuit){
    
    			System.exit(0);
    		}
    		if(ae.getSource() == mnuItemAbout){
    
    			JOptionPane.showMessageDialog(null, "Sistem Pelepasan Pelajar version 1.0 ");
    		}
    	}
    	public static void send()
    	{
    		emailprocess lala = new emailprocess();
    		lala.postMail(receivertext,sendertext,smtptext);
    		//lala.postMail("saya","la","ha");
    	}
      }
    **************
    
    //2nd class emailprocess.java
    
    
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    import java.io.*;
    
    
    
    public class emailprocess
    {
    
      public static String SMTP_HOST_NAME;// = "smtp.gmail.com";
      public static String SMTP_AUTH_USER = "itpsm2012.1";
      public static String SMTP_AUTH_PWD  = "itpsm2012.123456789";
    
      public static String emailMsgTxt      = "Title";
      public static String emailSubjectTxt  = "body";
      public static String emailFromAddress;// = "itpsm2012.1@gmail.com";
    
      // Add List of Email address to who email needs to be sent to
      public static String emailList;// = {"lol_player01@yahoo.com", "lol_player02@yahoo.com"};
    
    
    
      /*public static void main(String args[]) throws Exception
      {
        emailprocess smtpMailSender = new emailprocess();
        smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
      }*/
    
      public void postMail( String recipients, String sender,
                                String smtp) throws MessagingException
      {
      	emailList = recipients;
      	emailFromAddress = sender;
      	SMTP_HOST_NAME = smtp;
    
        boolean debug = false;
    
         //Set the host smtp address
         Properties props = new Properties();
         props.put("mail.smtp.host", SMTP_HOST_NAME);
         props.put("mail.smtp.auth", "true");
         props.put("mail.smtp.starttls.enable", "true");
         props.put("mail.smtp.port", "587");
         props.put("mail.debug", "true");
    
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
    
        session.setDebug(debug);
    
        // create a message
        Message msg = new MimeMessage(session);
    
        // set the from and to address
    	Address a = new InternetAddress(emailFromAddress);
        Address b = new InternetAddress(emailList);
    
    
    
        // Setting the Subject and Content Type
        msg.setContent("message", "text/plain");
        msg.setFrom(a);
        msg.setRecipient(Message.RecipientType.TO, b);
        msg.setSubject("subject");
        Transport.send(msg);
     }
    
    
    /**
    * SimpleAuthenticator is used to do simple authentication
    * when the SMTP server requires it.
    */
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
    
        public PasswordAuthentication getPasswordAuthentication()
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
    
    }
    **************
    driver main is in another class.

    this is the error i get while compiling, in emai.java code :


    error: method postMail in class emailprocess cannot be applied to given types;

    any help would be appreciated.
    Last edited by Norm; 03-19-2012 at 09:45 PM. Reason: added code tags

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

    Default Re: Please help me in javax.mail to send a email..

    Can you post the full text of the error message that shows all the info from the compiler?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default Re: Please help me in javax.mail to send a email..

    lala.postMail(receivertext,sendertext,smtptext)
    You are passing in Arguments of the type Jbutton into a method that expects Strings...

  4. #4
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Please help me in javax.mail to send a email..

    this is the only error i get :

    error: method postMail in class emailprocess cannot be applied to given types; line 121

  5. #5
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Please help me in javax.mail to send a email..

    actually i am passing arguments of type JTextField.

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

    Default Re: Please help me in javax.mail to send a email..

    Can you post the FULL text of the error message? Do not edit out parts of it.
    Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    The postMail() method takes String arguments. What are the types of the args you are passing it?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Please help me in javax.mail to send a email..

    Sorry for abandoning post~ problem solved already, regarding try and catch.

Similar Threads

  1. javax.mail.Session cannot be cast to javax.mail.Session
    By JustinCase in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-26-2012, 05:43 PM
  2. Replies: 10
    Last Post: 06-06-2010, 06:49 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. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 05:55 PM
  5. Replies: 2
    Last Post: 05-05-2008, 06:01 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
  •