`

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/Lin

阅读更多

发送邮件:

 

package com.demo.common.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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);     
        }     
             
    }     
}

 

 

如果你用myEclipse进行开发的话,运行时可能会出现以下的Exception错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

 

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream  
        at 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)  

 

原因是jar包版本不统一,解决方法如下:
删除MyEclipse下JavaEE  Libraries/javaee.jar/mail里的包有东西.导入mail.jar

 

MyEclipse6.6具体方法如下:
用rar打开D:\Program Files\MyEclipse 6.6\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.6.0.zmyeclipse660200810\data\libraryset\EE_5/javaee.jar,删除其中的mail目录

MyEclipse8.5具体方法如下:
用rar打开D:\Program Files\MyEclipse 6.6\myeclipse\eclipse\plugins \com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset \EE_5\javaee.jar,删除其中的mail目录

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics