How to get access to the mailbox where user id contains space in it?

Chandra Patni

For JavaMail 1.2, you don't have do anything for imap authentication if username contains white spaces. You should add double quotes around user name for InternetAddress and for client which doesn't do this automcatically. For example, an imap session using telnet on port 143 would be as follows.

Client>
a001 LOGIN "Chandra Patni" javaiscool
Server>
a001 OK LOGIN completed.
Client>
a002 SELECT INBOX
Server>
* 4 EXISTS
* 0 RECENT
* FLAGS (Seen Answered Flagged Deleted Draft)
* OK [PERMANENTFLAGS (Seen Answered Flagged Deleted Draft)]
* OK [UNSEEN 2] Is the first unseen message
* OK [UIDVALIDITY 1803] UIDVALIDITY value.
a002 OK [READ-WRITE] SELECT completed.
Client>
a003 LOGOUT
However, the SMTP server seem to have problem with such addresses. I could not make sendmail/reply work. The following example connects to an Exchange server and creates a Message which is appended to INBOX.
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class TestMail1 {
  public static void main (String args[]) throws Exception {
    String username = "Java Duke";
    String password = "welcomeduke";
    String host = "myimap.domain.com";
    if(args.length == 3) {
        username = args[0];
        password = args[1];
        host = args[2];
    }
    Session session = Session.getInstance(new Properties(), null);
    MimeMessage message = new MimeMessage(session);
    InternetAddress from = new InternetAddress(""Java Duke"@"+ host);
    InternetAddress to = new InternetAddress(""Java Duke"@"+ host);
    message.setFrom(from);
    message.addRecipient(Message.RecipientType.TO, to);
    message.setSubject("This message is just added");
    message.setText("Welcome Duke");


    Store store = session.getStore("imap");
    store.connect(host, username, password);

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.appendMessages(new Message[] {message});


    Message messages[] = folder.getMessages();
    for (int i=0, n=messages.length; i<n; i++) {
       System.out.println(i + ": " + messages[i].getFrom()[0] + "	" + messages[i].getSubject());
    }
  }
}
The output and command is as follows
>java TestMail1 "Chandra Patni" welcome1 localhost
0: Chandra Patni <"Chandra Patni"@localhost>  test
1: <"Java Duke"@localhost> This message is just added
2: <"Java Duke"@localhost> This message is just added
3: <"Java Duke"@localhost> This message is just added
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact