Java不能发邮件并且报Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/m
public class MailClient { public void sendMail(String mailServer, String from, String to, String subject, String messageBody) throws MessagingException, AddressException { // Setup mail server Properties props = System.getProperties(); props.put("mail.smtp.host", mailServer); // Get a mail session Session session = Session.getDefaultInstance(props, null); // Define a new mail message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); // Create a message part to represent the body text BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(messageBody); // use a MimeMultipart as we need to handle the file attachments Multipart multipart = new MimeMultipart(); // add the message body to the mime message multipart.addBodyPart(messageBodyPart); // Put all message parts in the message message.setContent(multipart); // Send the message Transport.send(message); } public static void main(String[] args) { try { MailClient client = new MailClient(); String server="mail.gmail.com"; String from="johndoe@gmail.com"; String to = "johndoe@gmail.com"; String subject="Test"; String message="Testing"; client.sendMail(server,from,to,subject,message); } catch(Exception e) { e.printStackTrace(System.out); } } } Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStreamat javax.mail.Session.loadProvidersFromStream(Session.java:928)at javax.mail.Session.access$000(Session.java:174)at javax.mail.Session$1.load(Session.java:870)at javax.mail.Session.loadResource(Session.java:1084)at javax.mail.Session.loadProviders(Session.java:889)at javax.mail.Session.<init>(Session.java:210)at javax.mail.Session.getDefaultInstance(Session.java:299)at javax.mail.Session.getDefaultInstance(Session.java:339)at com.happy.mail.SendMail.send(SendMail.java:58)at com.happy.mail.SendMail.main(SendMail.java:37)
"D:\Java\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.0.me201007292038\data\libraryset\EE_5\javaee.jar"
// 打开上面的文件夹,然后删除里面的mail文件夹,OK!