Results 1 to 2 of 2

Thread: Sending Email (Java Program)

  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    66

    Sending Email (Java Program)

    I am creating my own program that will send email out. I have a GUI that calls the frame emailFrame(); which has the Email code in it.
    My problem is that I think my code is correct, but I have no mail server to connect to. Every SMTP server I try to connect to requires authentication. I don't know how to authenticate using my program. Is there anything I can add or alter in my code to get this to work, or possibly a free smtp server that doesn't require the AUTH command?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class emailFrame extends JFrame implements ActionListener
    {
      // Keep a reference to the applet parent.
    
    	JPanel n,s,outer;
    	JLabel lblEaddress, lblCC, lblSubject, lblFrom;
    	JTextField txtEaddress, txtCC, txtSubject, txtFrom;
    	JTextArea txtBody;
    	JButton btnEsend;
    	String Eaddress, CC, Subject, Body, From,smtpServer;
    
      public emailFrame()
      {
    	Container c = getContentPane();
    	setTitle("Compose Email");
            lblFrom = new JLabel("From?");
            txtFrom = new JTextField(20);
    	lblEaddress = new JLabel("Address:");
    	txtEaddress = new JTextField(20);
    	lblCC = new JLabel("CC:");
    	txtCC = new JTextField(20);
    	lblSubject = new JLabel("Subject:");
    	txtSubject = new JTextField(20);
    	txtBody = new JTextArea("",5,50);
    	btnEsend = new JButton("Send");
    	btnEsend.addActionListener(this);
    
    	n = new JPanel();
    	n.setLayout(new GridLayout(4,2));
    	n.add(lblEaddress);
    	n.add(txtEaddress);
    	n.add(lblCC);
    	n.add(txtCC);
    	n.add(lblSubject);
    	n.add(txtSubject);
    
    	s = new JPanel();
    	s.setLayout(new GridLayout(2,1));
    	s.add(txtBody);
    	s.add(btnEsend);
    
    	outer = new JPanel(new BorderLayout());
    	outer.add(n, BorderLayout.NORTH);
    	outer.add(s, BorderLayout.CENTER);
    	c.add(outer);
    
      }
    
      public void actionPerformed(ActionEvent evt)
      {
    	if(evt.getSource()==btnEsend)
    	{
                    From = (txtEaddress.getText());
    		Eaddress = (txtEaddress.getText());
    		CC = (txtCC.getText());
    		Subject = (txtSubject.getText());
    		Body = (txtBody.getText());
                    sendMail(From, Eaddress, CC, Subject, Body);
            }
      }
      
    public void sendMail(String From, String Eaddress, String CC, String Subject, String Body)
      {
          try {
        Socket s = new Socket("smtp.server.com", 25);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
          
        //Send username to mail server
        sendln(in, out, "HELO santa@npole.com");
        sendln(in, out, "MAIL FROM: <santa@npole.com>");
        sendln(in, out, "RCPT TO: <" + Eaddress + ">");
        sendln(in, out, "RCPT TO: <" + CC + ">");
        sendln(in, out, "DATA");
        sendln(out, Body);
        sendln(in, out, ".");
        sendln(in, out, "QUIT");
        s.close(); 
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
       public void sendln(BufferedReader in, BufferedWriter out, String s) {
      try {
        out.write(s + "\r\n");
        out.flush();
        // System.out.println(s);
        s = in.readLine();
        // System.out.println(s);
        }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
        public void sendln(BufferedWriter out, String s) {
       try {
        out.write(s + "\r\n");
        out.flush();
        System.out.println(s);
        }
       catch (Exception e) {
        e.printStackTrace();
        }
       }
    Let me know if you need to see my main frame where emailFrame() gets called.

  2. #2
    Member
    Join Date
    Apr 2006
    Posts
    66
    OK I finally figured it out... NOW the only question left remaining is how to send a carbon copy.
    i tried this
    sendln(in, out, "Cc:" + CC);
    but all it does is lock up

Posting Permissions

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