I not seem to be able to connect and send an email through a smtp server that uses CRAM-MD5 for authentication.
I keep on getting the error:
---
com.sun.mail.smtp.SMTPSendFailedException: 530 authentication required for mail submission
----
Please some tips/help on how to solve this ?
Below you find the debug output.
I also tried it with other peaces snippes ot code that I found on the internet, but it keeps on giving me the same error.
The piece of code I use:
Properties properties = System.getProperties();
properties.put("mail.smtp.host", server);
properties.put("mail.smtp.auth", "true");
Session ses = Session.getInstance(properties);
ses.setDebug(true);
MimeMessage msg = new MimeMessage(ses);
msg.setFrom(new InternetAddress(fromAddress));
msg.addRecipients(Message.RecipientType.TO, toAddress);
msg.setContent(body, "text/plain");
msg.setSubject(subject);
msg.saveChanges();
Transport tr = ses.getTransport("smtp");
// I am not sure about the one below, but it has no effect on the error
((SMTPTransport) tr).setSASLRealm("CRAM-MD5");
tr.connect(server, userName, password);
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
The debug output:
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.6.0_03\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.6.0_03\jre\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "HOST", port 25, isSSL false
220 ontrob1.bmsg.nl ESMTP Service (7.0.043) ready
DEBUG SMTP: connected to host "HOST", port: 25
EHLO laptop-ed
250-ontrob1.bmsg.nl
250-DSN
250-8BITMIME
250-HELP
250-AUTH CRAM-MD5
250-STARTTLS
250 SIZE 2097152
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Found extension "AUTH", arg "CRAM-MD5"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "SIZE", arg "2097152"
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: use8bit false
MAIL FROM:<CFT00290@cft.obebd.nl>
530 authentication required for mail submission
com.sun.mail.smtp.SMTPSendFailedException: 530 authentication required for mail submission
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at com.bv.taxes.Mailer.sendMail(Mailer.java:64)
at com.bv.taxes.IbSenderTest.bla(IbSenderTest.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:473)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:567)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:834)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:689)
at org.testng.TestRunner.privateRun(TestRunner.java:566)
at org.testng.TestRunner.run(TestRunner.java:466)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:301)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:296)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:276)
at org.testng.SuiteRunner.run(SuiteRunner.java:191)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:808)
at org.testng.TestNG.runSuitesLocally(TestNG.java:776)
at org.testng.TestNG.run(TestNG.java:701)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
FAILED: bla
com.sun.mail.smtp.SMTPSendFailedException: 530 authentication required for mail submission
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at com.bv.taxes.Mailer.sendMail(Mailer.java:64)
at com.bv.taxes.IbSenderTest.bla(IbSenderTest.java:90)
... Removed 22 stack frames
The JavaMail source code is available;
see the JavaMail web page.
You could start by modifying the SMTP
provider to clone the DIGEST-MD5 support
and convert it to CRAM-MD5 support.
What really needs to be done, and what I
plan to do in a future release, is to convert
the SMTP provider to use the JDK's SASL
API so that it can handle any SASL supported
mechanism. The IMAP provider already
does this.
Tanx for the feedback.
1) I had the same thoughts but hoped to get some more tips and inside information to where to start. So if you have them, please tell me, as it might save me some search/trial and error ?
I mean: I suppose, just like the digest-md5 support, this class is registered somewhere ? any idea where ?. How does it perform the digest-md5 authentication when this is encountered en requested by the server ?.... So basically the process flow... I don't think the coding is so difficult. I already looked in some coding of a sourceforge project called FreeMail that supports cram-md5 authentication. But I like to stick to sun javamail...
2) I will have a look at this sasl, as I saw it comming by when using imap to fetch email...
Hmmm... just came to think about it... I also need CRAM-MD5 for my imap connection with the client.... almost forgot that :(... is that supported ?
What do you think is the "easiest" way to go ?: 1) or 2).. and where to find examples ?
See the code in the com.sun.mail.smtp
package, in particular the SMTPTransport
class. Look for the string "MD5". There's
also a separate DigestMD5 class that has
the real implementation.
As I said, the IMAP provider uses the SASL
API. I believe the JDK SASL implementation
includes support for CRAM-MD5, but you'll
want to check their documentation to be sure.
Sorry, forgot to mention: I already downloaded the source as I needed to cutomize a few things earlier... like the proper response from the server, etc...
But i will have further look at it. Just to hoped you had some more inside info that could get me up to speed quickly...
I will first make an invetarisation of both options and choose one.
BTW: The SASL supports CRAMD-MD5 according to their api spec: http://java.sun.com/j2se/1.5.0/docs/guide/security/sasl/sasl-refguide.html