Hot questions for Using Ubuntu in javamail

Question:

I am trying to connect to a local postfix+dovecot server using javamail. I have connected to the server successfully using Thunderbird so I know that I have the username and password correct, but I always get

Javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Authentication failed. 
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:661)
at javax.mail.Service.connect(Service.java:295)
at servers.MailEngine.initMailServer(MailEngine.java:661);

The only thing that I know that I have done is change the java.security to disable some less secure protocols. what protocols does dovecot need? or am I looking the wrong direction with this.

If this is a protocol problem is there any way that I can pass a parameter to the compiler? I do have other services that I would like to keep from supporting the disabled protocols

EDIT:

Session emailSessionObj;
Store storeObj;
Properties props = new Properties();
props.put("mail.imap.host", 127.0.0.1);
props.put("mail.imap.port", 143);

emailSessionObj = Session.getDefaultInstance(props);
storeObj = emailSessionObj.getStore("imap");

storeObj.connect("127.0.0.1", 143, "username","password");

Here is the code that I used

And Here is the debug output

DEBUG: setDebug: JavaMail version 1.5.0-b01
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
localhost : 143 : from@smsmail.pittsburgfoundry.com : smsmailpass:DEBUG IMAP: trying to connect to host "localhost", port 143, isSSL false
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN] Dovecot (Ubuntu) ready.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=localhost, user=from@smsmail.pittsburgfoundry.com, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A0 NO [AUTHENTICATIONFAILED] Authentication failed.

Answer:

In the end It ended up being a combination of an outdated javamail and some problems with my map authentication.

Another problems was that the username was enterd as xxx@yyy.zzz and it simply wanted xxx. The debug message from dovecot and pam were too vague to be very helpful as it did not find a username

Question:

I tried the following code from a random site to test sending email from java in ubuntu 16.04 I have included the the mail.api,smtp.jar

    package com.javapapers.java;
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class JavaEmail {

    Properties emailProperties;
    Session mailSession;
    MimeMessage emailMessage;

    public static void main(String args[]) throws AddressException,
            MessagingException {

        JavaEmail javaEmail = new JavaEmail();

        javaEmail.setMailServerProperties();
        javaEmail.createEmailMessage();
        javaEmail.sendEmail();
    }

    public void setMailServerProperties() {

        String emailPort = "587";//gmail's smtp port

        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", emailPort);
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");

    }

    public void createEmailMessage() throws AddressException,
            MessagingException {
        String[] toEmails = { "joe@javapapers.com" };
        String emailSubject = "Java Email";
        String emailBody = "This is an email sent by JavaMail api.";

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        for (int i = 0; i < toEmails.length; i++) {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html");//for a html email
        //emailMessage.setText(emailBody);// for a text email

    }

    public void sendEmail() throws AddressException, MessagingException {

        String emailHost = "smtp.gmail.com";
        String fromUser = "your emailid here";//just the id alone without @gmail.com
        String fromUserEmailPassword = "your email password here";

        Transport transport = mailSession.getTransport("smtp");

        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }

}

I am getting the following error (javac shows no error)

Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS;

I am just a beggineer in linux .Please provide any commands which may help.


Answer:

Please add and check

emailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Update:
public void setMailServerProperties() {

    String emailPort = "587";//gmail's smtp port

    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.starttls.enable", "true");
    emailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com"); // add here

}

Resource Link: JavaMail API – Sending email via Gmail SMTP example

Question:

I have email on my Ubuntu box coming into a directory like /var/mail. In there is a file called "root" that contains all received emails. I use the "mail" command to access these emails in a more user-friendly way. Is there any way to configure JavaMail to read these emails without having to parse the "root" email file like a text file?


Answer:

JavaMail can parse the Unix mail file for you using the "mbox" provider.