JavaMail Authentication (Newbie)

Hi all,
When specifying Javamail properties when obtaining a session, e.g:
Properties p = new Properties();
p.put("mail.store.protocol", "imap");
Session.getInstance(p, null);... is it possible to specify a username and password in the properties object when the mail server requires authentication, e.g.
Properties p = new Properties();
p.put("mail.store.protocol", "imap");
p.put("username", "my_username");
p.put("password", "my_password");
Session.getInstance(p, null);

Hi all,
When specifying Javamail properties when obtaining a session, e.g:
Properties p = new Properties();
p.put("mail.store.protocol", "imap");
Session.getInstance(p, null);... is it possible to specify a username and password in the properties object when the mail server requires authentication, e.g.
Properties p = new Properties();
p.put("mail.store.protocol", "imap");
p.put("username", "my_username");
p.put("password", "my_password");
Session.getInstance(p, null);

Similar Messages

  • Basic Javamail Authentication

    Hi,
    Could somebody tell me what is wrong with the following code? I have to authenticate myself to receive a email since I am inside a firewall..
    The following is what I have right now....
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Properties;
    import javax.servlet.*;
    public class MailMessage {
    public MailMessage(String to, String subject, String bodyText)
    throws ServletException
    try
    String host = "smtp of my server";
    String from = "[email protected]";
    String toAddress = "[email protected]";
    String cc = "[email protected]";
    // Get system properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    // Only allow registered users to send mail
    props.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(props, new MyAuthenticator());
    session.setDebug(true);
    // Define message
    MimeMessage message = new MimeMessage(session);
    // To
    InternetAddress Address = new InternetAddress(toAddress);
    message.addRecipient(Message.RecipientType.TO, toAddress);
    // From
    InternetAddress fromAddress = new InternetAddress(from);
    message.setFrom(fromAddress);
    message.setSubject(subject);
    message.setText(bodyText);
    Transport transport = session.getTransport("smtp");
    // Send message
    transport.send(message);
    catch (Exception e)
    throw new ServletException(
    "<b>Email Message not sent. Contact admin </b>\n" + e);
    class MyAuthenticator extends Authenticator
    public PasswordAuthentication getPasswordAuthentication()
    String userName = "Administrator";
    String password = "openup" ;
    return new PasswordAuthentication(userName, password);
    I get the following error which I don't understand..
    addRecipient(javax.mail.Message.RecipientType, javax.mail.Address) in javax.mail.Message cannot be applied to (javax.mail.Message.RecipientType, java.lang.String)
    What does this mean? Am I not using the authenticator class properly? I am lost now!
    Thanks for any help!

    1) He's not explicitly casting to (InternetAddress)
    2) You can't cast a String to an InternetAddress anyway.
    He did the right thing in creating a new InternetAddress from his toAddress (String), but he just passed the wrong one in to the function: it only takes an InternetAddress (not a String).
    Let's do it differently:
    String strTo... // String representation of recipient passed in
    InternetAddress addrTo; // this is what we need for the function
    addrTo = new InternetAddress(strTo);
    // here's the original equivalent
    message.addRecipient(Message.RecipientType.TO, strTo); // wrong. this doesn't take a String
    // needs this:
    message.addRecipient(MessageRecipientType.TO,addrTo); // yep. takes InternetAddress.

  • JavaMail Authenticator?

    * Created on 2003/10/29
    * To change the template for this generated file go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    package com.genech.mail;
    * @author ppgoodboy
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    public class SimpleSender
         public static void main(String[] args)
              try
                   String smtpServer = args[0];
                   String to = args[1];
                   String from = args[2];
                   String subject = args[3];
                   String body = args[4];
                   send(smtpServer,to,from,subject,body);
                   send("smtp.sohu.com",
                         "[email protected]",
                         "[email protected]",
                         "test",
                         "chou san this is nothing , only test!"
              catch(Exception ex)
                   System.out.println("Usage: java com.genech.mail.SimpleSender" + "smtpServer toAddress from Address subjectText bodyText");
              System.exit(0);
         public static void send(String smtpServer,String to ,String from,String subject ,String body)
              try
                   MyAuthenticator auth = new MyAuthenticator ("username","password");          
                   Properties props = System.getProperties();
                   props.put("smtp.sohu.com",smtpServer);
                   Session session = Session.getDefaultInstance(props,auth);
                   Transport tr = session.getTransport("smtp");
                   tr.connect(smtpServer, "username", "password");
                   Message msg = new MimeMessage(session);
                   msg.setFrom(new InternetAddress(from));
                   msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
                   msg.setSubject(subject);
                   msg.setText(body);
                   msg.setHeader("X-Mailer","GENECH COMMUNICATION MAIL");
                   msg.setSentDate(new Date());
                   msg.saveChanges();                    
                   tr.sendMessage(msg,msg.getAllRecipients());
                   tr.close();
                   //Transport.send(msg);
                   System.out.println("Message sent OK");
              catch(MessagingException e)
                   e.printStackTrace();
                   System.out.println("I do not know why the mistake!");
              catch(Exception e)
                   e.printStackTrace();
    class MyAuthenticator extends Authenticator
         String smtpUsername = null;
         String smtpPassword = null;
         public MyAuthenticator(String username, String password)
              smtpUsername = username;
              smtpPassword = password;
         protected PasswordAuthentication getPasswordAuthentication()
              return new PasswordAuthentication(smtpUsername,smtpPassword);
    /*     public PasswordAuthentication getPasswordAuthentication()
              return new PasswordAuthentication( "username", "password"); 
    javax.mail.MessagingException: 505 Error: Client was not authenticated
         at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
         at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
         at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
         at com.genech.mail.SimpleSender.send(SimpleSender.java:77)
         at com.genech.mail.SimpleSender.main(SimpleSender.java:34)
    Who can help me ? Why can't I use the Authenticator succesfully?

    Hi,
    There is no error in your code. Check your mail server host. Instead you can directly replace with IP.
    Hope this helps..
    -Kavitha

  • JavaMail Authentication

    Hi,
    I have a big problem here. I need to authenticate the username and password before my email server allows me to send out emails. But i always seems to get : 550 user not local : Authentication required for relay.
    Below are part of my Email class where i use Authenticator class
    Properties props = new Properties();
    props.put("mail.smtp.host", "123.123.123.3");
    Authenticator auth = new SimpleAuthenticator();
    session = Session.getDefaultInstance(props, auth);
    session.setDebug(true);
    Below are the codes for SimpleAuthenticator class
    public class SimpleAuthenticator extends Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
    String username, password;
    username = "admin";
    password = "adminpassword";
    return new PasswordAuthentication(username, password);
    Is there anything wrong with the above codes ?? Please advise

    Doesn't anybody search the forum before asking the same old questions time and time again???
    Look at this thread
    http://forum.java.sun.com/thread.jsp?forum=43&thread=252826
    SH

  • JavaMail Authentication problem

    I try to send a message using yahoo's smtp server. This server requires authentication. When I set the debug flag on for the session object, I see that yahoo's server responding for the authentication with OK, but my send still fails with an AuthenticationFailedException. Any idea?
    Here is the code and the output of the debug session:
    // set properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "smtp.mail.yahoo.com");
    props.put("mail.smtp.auth", "true");
    // Get a Session object
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(true);
    // create message
    Message msg = new MimeMessage(session);
    // set to, from, etc.
    //try to connect and send it
    msg.saveChanges();
    Transport tp = session.getTransport("smtp");
    tp.connect("smtp.mail.yahoo.com", user, password);
    // send the thing off
    tp.send(msg, msg.getAllRecipients());
    tp.close();
    Here is the debug log:
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG: SMTPTransport trying to connect to host "smtp.mail.yahoo.com", port 25
    DEBUG SMTP RCVD: 220 smtp011.mail.yahoo.com ESMTP
    DEBUG: SMTPTransport connected to host "smtp.mail.yahoo.com", port: 25
    DEBUG SMTP SENT: EHLO wva24-d9kzst01
    DEBUG SMTP RCVD: 250-smtp011.mail.yahoo.com
    250-AUTH=LOGIN PLAIN
    250-PIPELINING
    250 8BITMIME
    DEBUG SMTP Found extension "AUTH=LOGIN", arg "PLAIN"
    DEBUG SMTP Found extension "PIPELINING", arg ""
    DEBUG SMTP Found extension "8BITMIME", arg ""
    DEBUG SMTP: Attempt to authenticate
    DEBUG SMTP use AUTH=LOGIN hack
    DEBUG SMTP SENT: AUTH LOGIN
    DEBUG SMTP RCVD: 334 VXNlcm5hbWU6
    DEBUG SMTP SENT: bGFzemxvX3pla2U=
    DEBUG SMTP RCVD: 334 UGFzc3dvcmQ6
    DEBUG SMTP SENT: Z3JlZW5kdWNreQ==
    DEBUG SMTP RCVD: 235 ok, go ahead (#2.0.0)
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    javax.mail.SendFailedException: Sending failed;
    nested exception is:
         javax.mail.AuthenticationFailedException

    Replace
    // Get a Session object
    Session session = Session.getDefaultInstance(props, null);
    with
         SimpleAuthenticator auth = new SimpleAuthenticator(new javax.swing.JFrame());
    Session session = Session.getDefaultInstance(props, auth);
    It then prompts you for the userid and password.
    Hope it help

  • Email server needs authentication

    I want to send an email from a Creator project, but my email server requires authentication. The sun.net.smtp.SmtpClient does not seem to provide an API for that.
    Can anyone supply a solution?

    I want to send an email from a Creator project, but
    my email server requires authentication. The
    sun.net.smtp.SmtpClient does not seem to provide an
    API for that.
    Can anyone supply a solution?Try JavaMail.
    http://java.sun.com/products/javamail/index.jsp
    It has the bells and whistles that allow you to provide authentication. Search/google the web for
    "Javamail authentication" and look for examples.
    It's not as simple to use as SmtpClient, but has the
    flexibility to do many things.
    -Joel

  • My app store is not working after installing mavericks. When I open app store it repeatedly asking me to login with apple ID and to provide User name and Password for proxy authentication in a loop.I am a newbie to mac,Please help me.

    My app store is not working after installing mavericks. When I open app store it repeatedly asking me to login with apple ID and to provide User name and Password for proxy authentication in a loop.I am a newbie to mac,Please help me.

    Hmmmm... would appear that you need to be actually logged in to enable the additional menu features.
    Have you tried deletting the plists for MAS?
    This page might help you out...
    http://www.macobserver.com/tmo/answers/how_to_identify_and_fix_problems_with_the _mac_app_store
    Failing that, I will have to throw this back to the forum to see if anyone else can advise further.
    Let me know how you get on?
    Thanks.

  • JavaMail: How to tell if SMTP server requires authentication

    I am writing an application that sends notification emails. I have a configuration screen for the user to specify the SMTP hostname and optionally a username and password, and I want to validate the settings. Here is the code I am using to do this:
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    if (mailUsername != null || mailPassword != null)
        props.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(props, null);
    transport = session.getTransport();
    transport.connect(mailHostname, mailUsername, mailPassword);
    transport.close();This works if the user enters a username and password (whether they are valid or not), or if the username and password are empty but the SMTP server does not require authentication. However, if the server requires authentication and the username and password are empty, the call to transport.connect() succeeds, but the user will get an error later on when the app tries to actually send an email. I want to query the SMTP server to find out if authentication is required (not just supported), and inform the user when they are configuring the email settings. Is there any way to do this? I suppose I could try sending a test email to a dummy address, but I was hoping there would be a cleaner way.

    Thanks for your help. This is what I ended up doing, and it seems to work. For anyone else interested, after the code above (before transport.close(), I try to send an empty email, which causes JavaMail to throw an IOException, which I ignore. If some other MessagingException occurs, then there is some other problem (authentication required, invalid from address, etc).
    try
       // ...code from above...
       // Try sending an empty  message, which should fail with an
       // IOException if all other settings are correct.
       MimeMessage msg = new MimeMessage(session);
       if (mailFromAddress != null)
           msg.setFrom(new InternetAddress(mailFromAddress));
       msg.saveChanges();
       transport.sendMessage(msg,
           new InternetAddress[] {new InternetAddress("[email protected]")});
    catch (MessagingException e)
        // IOException is expected, anything else (including subclasses
        // of IOException like UnknownHostException) is an error.
        if (!e.getNextException().getClass().equals(IOException.class))
            // Handle other exceptions
    }Edited by: svattom on Jan 7, 2009 7:37 PM
    Edited by: svattom on Jan 7, 2009 10:01 PM
    Changed handling of subclasses of IOException like UnknownHostException

  • Newbie XSan question - authenticating a computer from XSan Admin

    Hello - I have a very newbie XSan question!
    I have 2 computers that connect to an XSan. 1 I am having no problems with. The other I am unable to connect with.
    When I look in XSan Admin the 2nd computer (the one that is not working) is not authenticated.
    So I click on it and choose Authenticate. I enter what I know is an Admin username and password for the computer I am attempting to connect. The dot next to the computers IP turns green with 3 dots in it and then it turns gray again. It just does not connect.
    What is the problem?
    Is it licensing?
    Thanks
    Taj

    You need a different license ID for each machine, plus the admin machine. Do you have that?

  • Javamail check for no SMTP authentication required

    Some SMTP servers do not require authentication and with raw SMTP, you can send a MAIL FROM: <[email protected]> and if authentication is required, it will notify. However, I don't know if there is an easy built in way to do this with JavaMail (or if it is possible right now).
    Edited by: gl_sac on Sep 16, 2010 1:32 PM

    Thanks for the answer. Is there a way of doing it without sending the message? I wonder if I can just initiate sending to get the authentication error? Basically, if error - it will be interrupted by Authentication error. But if all goes well, it will be sent. I don't want it to be sent. I just wonder can we initiate sending, issue some commands (to provocate auth error), and if all goes good close connection (to avoid dummy mail to be sent). Here is how I saw that it could be done from telnet (at least on some servers) and wonder if it can be done in JavaMail:
    [note: gmail just used as example, it is a different server that I ran test on}
    MAIL FROM: <[email protected]>
    1. putty -telnet smtp.abc.com -P 25
    2. EHLO sfsdfs
    3. MAIL FROM: <[email protected]>
    554 5.7.1 <[email protected]>: Sender address rejected: Access denied
    The error is before DATA called.

  • J2ee security newbie: integrated authentication question

    I am trying to build a set of JSPs/servlets that require authentication and probably authorization. The jsp / sevlets should be able to authenticate against any underlying password system, or should cope with most common systems such as win2k / unix etc. I do not want to force the organisation to build a new database of users / passwords or to type in passwords in clear text in xml files.
    I would preferably like to use form-based authentication to avoid Http basic clear text password sending. This will also allow me to custmize the UI of the login screen.
    The solution should not be container specific. or at least the containers (tomcat + webspehere) should allow for it in their own way.
    After a lot of reseasrch on the web, I cant seem to find an accepted way of doing this. I would like comments on the choices I have made so far and the choices I should be making. Any links to reading material would be helpful. I would like to understand which lower level technologies to depend upon eg LDAP / Kerberos etc. Any help will be appreciated
    TIA,
    Zdz

    I would preferably like to use form-based authentication to avoid Http basic clear text password sending.
    This will also allow me to custmize the UI of the login screen. form based auth is just like basic auth. Both is sending userid and password in clear text. basic auth sends it base64 encoded in the http header. Form based auth sends it in the http message body.
    /Bo
    http://appliedcrypto.com

  • J2ee security newbie: integrated authentication help

    I am trying to build a set of JSPs/servlets that require authentication and probably authorization. The jsp / sevlets should be able to authenticate against any underlying password system, or should cope with most common systems such as win2k / unix etc. I do not want to force the organisation to build a new database of users / passwords or to type in passwords in clear text in xml files.
    I would preferably like to use form-based authentication to avoid Http basic clear text password sending. This will also allow me to custmize the UI of the login screen.
    The solution should not be container specific. or at least the containers (tomcat + webspehere) should allow for it in their own way.
    After a lot of reseasrch on the web, I cant seem to find an accepted way of doing this. I would like comments on the choices I have made so far and the choices I should be making. Any links to reading material would be helpful. I would like to understand which lower level technologies to depend upon eg LDAP / Kerberos etc. Any help will be appreciated
    TIA,
    Zdz

    Most container come with the ability to put security constraints in the web.xml file.
    Then, you can set up your container to do the authentication (tomcat call this Realm). And there is a JAAS realm that can be configured a little bit like PAM in Unix/Linux.
    There is also a Security Filter around on the net.
    Hope this helps.

  • Authentication Failed - newbie

    I am managing 4 other systems, which all have the same admin user name and password. 2 of the systems I cannot Observe/Control/Curtain. I can chat, message, install etc... Each time I double click on them I get 'Authentication Failed to "X Machine"'. I used to have access to all 4 workstations 2 weeks ago. Why did I loose access to these 2 machines? Nothing has been done to these systems to warrant this sudden loss of access. Any ideas?
    Sly
    Dual 2GHz Power PC G5   Mac OS X (10.4.8)  

    The two most likely cases are:
    1) Someone disabled the control/observe priveleges in the control panel.
    2) Port 5900 is being blocked (probably by the software firewall).

  • JavaMail Exchange Server Windows Integrated Authentication

    I need to send an email using Java Mail by Exchange Server that uses Windows Integrated Authentication.
    Is it possible? If so how?
    (I read some old posts and I get some info but I have to sure is it possible or not just sending mail)

    Hi, jeff81.
    I had same problem with Win2003 server. Try this:
    Start -> Settings -> Control Panel -> Administrative Tools -> Services
    then select "PROPERTIES/LOGON" for necessary service.
    Change "Local System account" to your user account.
    Make sure that user account have necessary grants.
    ps. sorry my poor english :(

  • (newbie)JWSDP JAXR Browser Authentication Failure

    Hi All,
    I've installed JWSDP 1.1. When using the JAXR Browser to add a company to the UDDI registry, it asks for a userid/password when I click the "Submit" button. No matter what I type in (or don't), the error is the same.
    Error Authenticating User
    java.xml.registry.SaveException: UDDI Disposition Report: Error Code = E_UNKNOWNUSER; Error Message = XMLDBUserAuth.getAuthentication(...); Error Number = 10500
    If I retry, the Error Code changes to E_FATALERROR.
    I'm sure there's a step that I've left undone, but I can't see what it is. Could someone please point me in the right direction? Where/How do I define the userid/password it wants?
    Thanks in advance
    Troy Rudolph
    Computer Associates

    Well, verify you have requested a publishing username & password..
    (however, I did that, but still got same message. I assume it might be due to proxy settings, however, I can query... weird)

Maybe you are looking for

  • Logging configuration is missing or invalid for target

    Hi, I am getting this error while starting the SOA Server HTTP to server: '[2001:0:cf2e:3096:30be:1076:3f57:fefc]', port: '7001' at weblogic.net.http.HttpClient.openServer(HttpClient.java:314) at weblogic.net.http.HttpClient.openServer(HttpClient.jav

  • Exchange support for categories in iCal?

    Just upgraded my MacBook to Snow Leopard, and immediately went about configuring it for use with my office's Exchange server. It works for the most part, but it seems that iCal does not support categories in the Exchange calendar. I rely on categorie

  • New power adapter fail

    My friend lost my original power adapter and she bought me a new adapter, however, this adapter causes me lots of problems-it works sometimes, but often I found it failed to work(the light is off, doesn't charge at all), you never know when it is gon

  • Illustrator will not let me copy in a screenshot on my mac

    I have screenshot lots of images and they will not let me drag them in to illustrator to use they save as a .png when i screenshot and it has always worked before but now wont it says 'an unknown format cannot be opened'

  • Sync: Error while syncing - Unknown Error

    Firefox 3.6.13 is on 2 XP computers, in home and in office. Both have Sync 1.6.1 with the same settings and same key. One works ok, but another fails to update every time. Tried with all add-ons but Sync disabled, did not help. Un/reinstalled Sync, d