
i am writting a program to send emails using gmail smtp server. I had the following error: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:620) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$000(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) Exception in thread "main" Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds) Could anyone help me correct this? here is the code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package JavaMail;
import MISC.Repository; import java.util.Properties; /* import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; */ import javax.mail.*; import javax.mail.internet.*; /** * * @author Aileen */ public class SendMail {
String username;
String password;
String mailServer;
Session session;
Message message;
public SendMail (String mServ, String un, String pw) {
username = un;
password = pw;
mailServer = mServ;
Properties props = new Properties ();
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
}
public void setFields (String sub, String msg, String hName, String hVal) {
try {
message = new MimeMessage(session);
message.setHeader(hName, hVal);
message.setSubject(sub);
message.setText(msg);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void mailer (String from, String to) {
try {
message.setFrom(new InternetAddress (from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
Transport.send(message);
Repository.showMessage("Message Successfully Sent to "+to, "SUCCESS", 'P');
}catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void main (String args[]) {
SendMail sm = new SendMail ("smtp.gmail.com", "elimence", "awonkyarok");
sm.setFields("HELLO", "DOES IT WORK", "HEADER", "HEADER VALUE");
sm.mailer("Sector 7", "fancymastaflexx@yahoo.co.uk");
}
}