我编写了一个代码来在hibernate中发送电子邮件。在java项目中,我添加了 - mail.jar
和javaee-api-6.0.jar
。我已经在堆栈溢出中搜索了解决方案/答案但是没有成功,即使在其他相关网站上也是如此。
如何解决错误:
ClassFormatError - 线程" main"中的例外情况java.lang.ClassFormatError:缺席代码 方法中的属性在类文件中不是本机的或抽象的 使用javax /邮件/认证者 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) 在java.net.URLClassLoader.access $ 100(未知来源) 在java.net.URLClassLoader $ 1.run(未知来源) 在java.net.URLClassLoader $ 1.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) 在java.lang.Class.getMethod0(未知来源) 在java.lang.Class.getMethod(未知来源) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) 在sun.launcher.LauncherHelper.checkAndLoadMain(未知来源)
我的java代码是:
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
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 Email {
public static void main(String[] args) {
try{
final String fromEmail="shrivastavasucheta@gmail.com";
final String password = "Virtual";
final String toEmail = "sucheta.docs@gmail.com";
System.out.println("TLS mail start");
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", true);
Authenticator auth = new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(fromEmail,password);
}
};
Session session =Session.getInstance(prop,auth);
MimeMessage message = new MimeMessage(session);
message.setFrom (new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO,new
InternetAddress(toEmail));
System.out.println("Mail InternetAddress and add recipient");
message.setSubject("Testing Campus");
message.setText("Hello");
System.out.println("Mail Subject");
Transport.send(message);
System.out.println("mail sent");
}
catch(Exception e){
System.out.println("mail fail");
System.out.println(e);
}
}
}
而且我必须为雅虎邮件实现代码,雅虎邮件包含yahoo.com和yahoo.co.in扩展。因为我有2个邮件ID用于“To'和'来自'电子邮件 - 选择哪一个?
prop.put("mail.smtp.host", "smtp.yahoo.com");
或:
prop.put("mail.smtp.host","smtp.yahoo.co.in");