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.
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 commandThen execute the java command to run your program
For more details on Error: Could not find or load main class in Java follow this link.