开发者

JavaMail to send email through vps, require NO authentication, but get AuthenticationFailedException - why?

开发者 https://www.devze.com 2023-02-01 18:55 出处:网络
I originally setup my site to use my local ISP to send email through my site. I would like to change this, and start sending email through my VPS. According to the online documentation (from my vps pr

I originally setup my site to use my local ISP to send email through my site. I would like to change this, and start sending email through my VPS. According to the online documentation (from my vps provider) I have "POP before SMTP authentication". So then, I do not require authentication nor do I require a secure connection at this time.

I changed a few things in the code I used for sending email through my local ISP. Here's what I have :

Transport t = null; 
   try {
    String Username = "mrsmith@mydomain.com";
    String Password = "somepassword";

        InternetAddress from = new InternetAddress("mrsmith@mydomain.com", "Bob Smith");                        

    Properties props = new Properties();                                    

   //commented out, since I don't want to use authentication
   //props.setProperty("mail.smtp.auth", "true");           

   props.put("mail.pop3.host", "mydomain.com";
   props.put("mail.smtp.host", "mydomain.com");

   String protocol = "smtp";             
   Session ssn = Session.getInstance(props, null);
   ssn.setDebug(true);  

 t = ssn.getTransport(protocol);    

Store s = ssn.getStore();
s.connect();

  t.connect(SMTP,Username,Password);

//Create the message
Message msg = new MimeMessage(ssn);
msg.setFrom(from);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setContent(body, "text/html");

t.sendMessage(msg, msg.getAllRecipients());
t.close();
s.close();  

I contacted my vps provider and after following their instructions, in tweaking the code, I still get "Authentication Failed". This after a successful connection has been made to the host on port 25.

Can someone point out what it is I'm missing here?

Editing to add additional information

After I added the store.connect("mailhost",Username,Password); statement to the code, I got further than ever before!

Here is some info that was printed to the tomcat prompt -

DEBUG : getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc.]
POP3: connecting to host "mydomain.com", port 110, isSSL false
server ready
OK User name accepted, password please
Password somepassword
OK Mailbox open, 0 messages

DEBUG: geProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smpt.SMTPTransport,Sun Microsystems, Inc.]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mydomain.com", port 25, isSSL false 
220 mydomain.com ESMTP Sendmail 8.14.3/8.14.3; DEC 2010 29
DEBUG SMTP: connected to host "mydomain.com", port: 25
( a lot of additional debug information was here )
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
535 5.7.0 authentication failed
javax.mail.AuthenticationFailedException
  at javax.mail.Service开发者_StackOverflow社区.connect(Service.java:319)
  at javax.mail.Service.connect(Service.java:169)
... continues with more 'at's.

Do you see anything odd in this information? I don't quite understand why the authentication failed while the Pop3 was authenticated with the same username and password.


POP before SMTP authentication basically means you need to be able to prove that you can fetch your mail using POP3 (using proper authentication) before you will be allowed to send mail using SMTP (without authentication).

Using the Java Mail API, you can do the authentication to the POP3 server like this :

Store store = ssn.getStore("pop3");
store.connect("mailhost", Username, Password);

You should execute these calls prior to connecting to the SMTP server, so before connecting to the SMTP server (without username and password) like this

t.connect();

make sure you connect to the store first.

In your snippet, you are still authenticating against the smtp as you are providing a username/password.

Another option is to use Commons Email, that has built-in support for pop before smtp authentication. Check out the following method in http://commons.apache.org/email/apidocs/org/apache/commons/mail/Email.html

email.setPopBeforeSMTP(true,popHost,popUsername,popPassword);

Using commons email, there's no need to do the pop plumbing in your code (connecting prior to sending).


Properties props = new Properties();
props.put("mail.smtp.host", "mailserver.com");
Session s = Session.getInstance(props,null);

InternetAddress from = new InternetAddress("mail@mail.com");
InternetAddress to = new InternetAddress(recepeint@server.com");

For more information please visit:

http://www.itpian.com/Coding/314-SENDING-EMAILS-THROUGH-JAVAMAIL.aspx


Check if provider implementation(pop3.jar) is in your buildpath/classpath.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号