I have made a simple java program that gets a user to fill out a form, and then checks if all the parts were filled out. I am now having trouble with how to send the file in an email to a set users email account. How could I do this? I still need to take the information (which I know how to do). So now I am having trouble with sending out the email. It doesn't have to be in java, but it should use my already created code.

Can anyone help me?

Thanks

One more thing .. If you have a firewall like Zonealarm etc , ignore the firewall warnings and allow your program to access smtp..

I have a problem with my code after I implemented the mail. This is the error that is gives.

DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
	java.net.ConnectException: Connection refused: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
	at javax.mail.Service.connect(Service.java:295)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at Main.sendMessage(Main.java:131)
	at Main.actionPerformed(Main.java:70)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(Unknown Source)
	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
	... 43 more

And here is the code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Main extends JApplet implements ActionListener{

	JTextField firstNameArea;
	JTextField lastNameArea;
	JTextField emailField;
	JTextField phoneNumber;
	JCheckBox myselfBox;
	JCheckBox someoneBox;
	JTextArea messageArea;
	
	
	public void init (){
		setSize (250,300);
		setLayout (new BorderLayout ());
		
		
		JPanel insidePane = new JPanel ();
		firstNameArea = new JTextField(15);
		lastNameArea = new JTextField (15);
		insidePane.setLayout (new GridLayout (7,2));
		insidePane.add (new JLabel ("First Name"));
		insidePane.add (firstNameArea);
		insidePane.add (new JLabel ("Last Name"));
		insidePane.add (lastNameArea);
		JButton sendButton = new JButton ("Send"); sendButton.setActionCommand ("Send Mail"); sendButton.addActionListener (this);
		emailField = new JTextField (15);
		insidePane.add (new JLabel ("Email"));
		insidePane.add (emailField);
		phoneNumber = new JTextField (15);
		insidePane.add (new JLabel ("Phone"));
		insidePane.add (phoneNumber);
		insidePane.add (new JLabel ("I am requesting "));
		myselfBox = new JCheckBox("Myself");
		someoneBox = new JCheckBox("Someone");
		insidePane.add (new JLabel ("this information for"));
		insidePane.add (new JLabel ());
		insidePane.add (myselfBox);
		insidePane.add (new JLabel ());
		insidePane.add (someoneBox);
		add (insidePane, BorderLayout.NORTH);
		
		JPanel messagePanel = new JPanel (new BorderLayout ());
		messageArea = new JTextArea (5,10);
		JScrollPane scrollPanel = new JScrollPane (messageArea);
		messagePanel.add (new JLabel ("Message"), BorderLayout.NORTH);
		messagePanel.add(scrollPanel, BorderLayout.CENTER);
		messagePanel.add(sendButton, BorderLayout.SOUTH);
		add (messagePanel, BorderLayout.CENTER);
		
		
		
	}
	
	public void actionPerformed (ActionEvent e){
		
		if (e.getActionCommand ().equals("Send Mail")){
			if (validEmailCheck (emailField.getText ())){
				if (validPhoneCheck (phoneNumber.getText())){
					if (oneBoxChecked ()){
						if (firstNameArea.getText() != null){
							if (lastNameArea.getText() != null){
								if (messageArea.getText() != null){
									sendMessage ();
								}
								else {
									JOptionPane.showMessageDialog(null, "Please Insert a Message");
								}
							}
							else {
								JOptionPane.showMessageDialog(null, "Please Insert Your Last Name");
							}
						}
						else {
							JOptionPane.showMessageDialog(null, "Please Insert Your First Name");
						}
					}
					else {
						JOptionPane.showMessageDialog(null, "Please select who you are writing for");
					}
					
				}	
				else {
					JOptionPane.showMessageDialog(null, "Please Enter a valid phone number (no dashes (-))");
				}
			}
			else {
				JOptionPane.showMessageDialog(null, "Please Insert a valid Email");
			}
			
		}
	
	}

	private void sendMessage() {
		// TODO Auto-generated method stub
		String to = "email@my-domain.com";
		String from = emailField.getText();
		String host = "http://webmail.sitegroundNUM.com/";
		
		Properties props = new Properties();
		
		 // If using static Transport.send(),
        // need to specify which host to send it to
        props.put("mail.my-domain.com:465", host);
        // To see what is going on behind the scene
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props);

        try {
            // Instantiate a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("Email from" + firstNameArea.getText() + " " + lastNameArea.getText());
            msg.setSentDate(new Date());

            // Set message content
            msg.setText("Phone:" + phoneNumber.getText() + "\n" + messageArea.getText());

            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
            mex.printStackTrace();
        }
		
	}

	private boolean oneBoxChecked() {
		// TODO Auto-generated method stub
		if (((myselfBox.isSelected()) || (someoneBox.isSelected())) || (myselfBox.isSelected() && someoneBox.isSelected())){
			return true;
		}
		else if (!myselfBox.isSelected() && !someoneBox.isSelected()){
			return false;
		}
		return false;
		
		
	}

	private boolean validPhoneCheck(String text) {
		// TODO Auto-generated method stub
		char [] phone = text.toCharArray();
		int trueCounter = 0;
		
		if (text.toCharArray().length == 10){
			for (int i = 0; i < 10; i++){
				checkIfValidNumbers (phone[i]);
				if (true){
					trueCounter += 1;
				}	
			}
		}
		if (trueCounter == 10){
			return true;
		}
		else {
		return false;
		}
	}

	private boolean checkIfValidNumbers(char c) {
		// TODO Auto-generated method stub
		switch (c){
		case 0:
			return true;
		case 1:
			return true;
		case 2:
			return true;
		case 3:
			return true;
		case 4:
			return true;
		case 5:
			return true;
		case 6:
			return true;
		case 7:
			return true;
		case 8: 
			return true;
		case 9:
			return true;
		default:
			return false;
		}
	}

	private boolean validEmailCheck(String text) {
	// TODO Auto-generated method stub
		
		
		char[] email = text.toCharArray();
		for (int i = 0; i < email.length; i++){
			if (email[i] == '@'){
				return true;
			}
		}
		return false;
	}
	
	
}

It seems like it does not allow my program to connect and send my email. I changed some of the connection areas around, but I will just fill in the domain when it works. I have an account with siteground. how could I get this program working?

Thanks for any help.

Looks like something went wrong...
You dont need to change any connections..
I will attach the entire working version of code if you would like to have...

I showed you what I used and my code and my error and I am wondering how to fix my code.

Line 111. Shouldn't that be props.put("mail.smtp.host", host); ?

That fixed the error. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.