문제

public class MailEx {
public static void main(String[] args) {
    try {

        String userName = "abc@gmail.com";          
        String password = "123";            
        String hostName = "smtp.gmail.com";
        String fromName = "Splendore Bkk";
        String to[] = {"xyz@gmail.com"};

        System.out.println("to.length::"+to.length);

        Properties props = new Properties();
        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");     
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        System.out.println("to.length:sadfsadfds:"+to.length);
        // Get the default Session object.
        Session session = Session.getInstance(props);

        // Create a default MimeMessage object.
        MimeMessage message1 = new MimeMessage(session);

        // Set the RFC 822 "From" header field using the
        // value of the InternetAddress.getLocalAddress method.
        message1.setFrom(new InternetAddress(userName,fromName));

        Address[] addresses = new Address[to.length];
        for (int i = 0; i < to.length; i++) {
            Address address = new InternetAddress(to[i]);               
            addresses[i] = address;
            // Add the given addresses to the specified recipient type.
            message1.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
        }       
        // Set the "Subject" header field.
        message1.setSubject("Testing");

        // Sets the given String as this part's content,
        // with a MIME type of "text/plain".
        Multipart mp = new MimeMultipart("alternative");
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setContent("Hii from cc", "text/html");
        mp.addBodyPart(mbp);
        message1.setContent(mp);
        message1.saveChanges();

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect(hostName,userName,password);
        transport.sendMessage(message1,addresses);
        transport.close();


    }catch (Exception e) {
        e.printStackTrace();
    }

}

}

오류가 발생합니다 ....

DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: ..\Java\jdk1.6.0\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
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.getInstance(Session.java:249)
    at com.test.MailEx.main(MailEx.java:41)

그럼 무엇이 문제인지 말해줄 수 있나요?

도움이 되었습니까?

해결책

피하려면 DEBUG 경고, 파일 생성 javamail.providers, javamail.address.map, javamail.default.address.map, javamail.default.providers 아래에

 (Program Files)\Java\jdk1.6.0\jre\lib\

폴더.

오류에 대해, NoClassDefFoundError, 글쎄, 당신은 클래스 경로에 JavaMail을 추가하지 않았습니다.Eclipse를 사용하는 경우 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 Build Path ≥ Add Libraries 또는 이와 유사한 항목을 따르고 javamail's를 추가합니다. jar 파일(귀하의 lib/ 폴더)를 프로젝트의 클래스 경로로 복사합니다.

다른 팁

이 솔루션이 있습니다

내 Ans 확인

이것은 문제에 대한 원하는 솔루션을 갖추고 있습니다

희망이 도움이 될 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top