I'm trying to mail from Java.
I'm getting this runtime exception:
java.lang.NoSuchMethodError: main. Exception in thread "main"
Cant figure out what the problem is. Please help.
I'm using the following code:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail{
public static void Mail(String from, String to, String subject, String text)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props, null);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try
{
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
Transport.send(simpleMessage);
} catch (MessagingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
String from = "prav.br@gmail.com";
String to = "prav.br@gmail.com";
String subject = "Test";
String message = "A test message";
Mail.Mail(from, to, subject, message);
}
}