Java send email, how to build the whole project

1k views Asked by At

I am trying to get javaMail to help send emails. I referred to examples to here: Send email using java I created a java project called mailtest, and pasted the code in the link to mailtest/src/SendEmail.java The code copied was changed to:

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail extends Object{

public static void main(String [] args)
{

    try{

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com"); // for gmail use smtp.gmail.com
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true"); 
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "my_real_yahoo_password");
            }
        });

        mailSession.setDebug(true); // Enable the debug mode

        Message msg = new MimeMessage( mailSession );

        //--[ Set the FROM, TO, DATE and SUBJECT fields
        msg.setFrom( new InternetAddress( "[email protected]" ) );
        msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("[email protected]") );
        msg.setSentDate( new Date());
        msg.setSubject( "Hello World!" );

        //--[ Create the body of the mail
        msg.setText( "Hello from my first e-mail sent with JavaMail" );

        //--[ Ask the Transport class to send our mail message
        Transport.send( msg );

    }catch(Exception E){
        System.out.println( "Oops something has gone pearshaped!");
        System.out.println( E );
    }
}
}

Then I

cd .../mailtest/src/
java SendEmail

It gives error of: Error: Could not find or load main class SendEmail.java

How should I set up the project please? There's no error signs in the file. Thank you very much.

3

There are 3 answers

0
Bacteria On

As per your code, my understanding is that you are using default package.So, 1st go to the mailtest-->src folder using cd command.

Type cd without parameters to display the current drive and directory and check whether you are in right directory(now you should be inside src folder) or not.

Next check where your SendEmail.class file is present or not in the same directory (src). If not then execute the javac command

javac SendEmail.java

Then execute the java command to run your program

java SendEmail

For more details on Error: Could not find or load main class in Java follow this link.

0
Rajesh On

You are trying to run program from command line. Below command should run as is :

java SendEmail

Now check where SendEmail.class file is located and run program from there. You can find that file using windows search. In case you are using Eclipse, your class file will be in below directory:

cd .../mailtest/bin/

If that's not your case then, you are missing current directory in classpath.

set classpath=%classpath%;.
1
pens-fan-69 On

Well, it looks like you haven't compiled the class. You should have a class file named SendMail.class in your current working directory given your current command line of java SendMail. You compile it using javac. I'd suggest that you take a look at the Getting Started Java tutorial. You will be better off starting there learning how to manually compile your source before graduating to using Eclipse.