質問

I am using javamail for the first time and having some exceptions which i dont understand i have seen some of those errors in other questions here as well but the answers to them does not help me. here is my code.

        final String username = "imsan1@cdcpk.com";
        final String password = "**********";
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "10.1.136.26");
        props.put("mail.smtp.port", "25");
        props.put( "mail.smtp.user" , username );
        props.put( "mail.smtp.password" , password );

        Session session = Session.getInstance(props,
          new SmtpAuthenticator(username, password)
          );


        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("imsan1@cdcpk.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("immni1@cdcpk.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

SmtpAuthenticator

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


class SmtpAuthenticator extends Authenticator {
     String user;
     String pw;
     public SmtpAuthenticator (String username, String password)
     {
        super();
        this.user = username;
        this.pw = password;
     }
    public PasswordAuthentication getPasswordAuthentication()
    {
       return new PasswordAuthentication(user, pw);
    }
}

Error log is

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST

    at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:64)
Caused by: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:59)

any help much appreciated

役に立ちましたか?

解決

The code is working fine the reason it was giving error was because i was not given rights to SMTP server hence the exception shown, after access was provisioned the mail was sent.

他のヒント

It appears that JavaMail is "out of sync" with the server, e.g., the server sent an invalid response, or just an unexpected blank line.

Can you post the entire debug output?

Also, try setting the property "mail.debug.quote" to "true"; it will provide more detail about the protocol communication.

Do you have an anti-virus or firewall product installed? Perhaps something is interposing on the SMTP protocol communication with the server and breaking the protocol in some way.

I think your mail server is exchange and it uses NTLM authentication (250-AUTH NTLM line), So you should send username in "domain\username" format to authenticate.

Also, mcafee by default blocks outgoing port 25 to stop spammer virüs/trojans. Disable "Prevent mass mailing worms sending mail" setting in "Anti-virus Standard Protection" part of Access Protection properties.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top