When I am trying to connect to an iCloud account using Javamail, it is returning an [AUTHENTICATION FAILED] error. This code worked for other non-OAuth providers as well (such as AOL, some Yahoo users). But, it is not working for iCloud.
Suggestions?
Properties props = new Properties();
props.put("mail.imaps.ssl.trust", "*");
Session session = Session.getInstance(props);
session.setDebug(true);
final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
store.connect("imap.mail.me.com", 993, <userEmail>#icloud.com, <non-null-password>);
Related
I am able to send mails using SMTP. But I need to implement Oauth to avoid using password while sending mails. This can be done using accesstoken.
After lot of surfing, i got below snippet.
Properties props = new Properties();
props.put("mail.smtp.user", userEmail);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = "";
transport.connect(host, port, userEmail, emptyPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
I am struggling to connect using SMTPTransport.connect with empty password.
Can anyone help me whether I can SEND_MAILS + SMTP + OAUTH + GMAIL_ACCESS_TOKEN?
I appreciate working code suggestions. Thanks
I use code from this link to access gmail imap server, because I could not find Android-friendly port of javamail with OAUTH support (javamail 1.5.2 or higher).
However, the problem with this code:
public static IMAPStore connectToImap(String host, int port, String userEmail, String oauthToken, boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
final String emptyPassword = "";
store.connect(host, port, userEmail, emptyPassword);
return store;
}
is that a new Store object is created every time auth token is changed (expires). And then I have to create a new Folder and read my messages again...
My question is:
Is it possible to change auth token without creating a new Store object? I would like to be able to implement something like
store.connect("imap.gmail.com", username, oauth2_access_token)
(example from javamail 1.5.2) to reconnect, without the need to recreate the Store object.
Thank you very much!
If you need to create a new connection with the same Store you should be able to set the property to a new value and make a new connection, without creating a new Store object. Just call props.put with the new value. The Session keeps a reference to the Properties object rather than making a copy of it.
Here is my code. The code is in groovy.
//send email
def host = "smtp.gmail.com"
def props = new Properties()
props.put "mail.smtps.auth", "true"
def session = Session.getDefaultInstance(props, null)
def msg = new MimeMessage(session)
msg.setSubject subject
msg.setText messageText
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, to))
def transport = session.getTransport "smtps"
transport.connect(host, username, password)
transport.sendMessage(msg, msg.getAllRecipients())
transport.close()
//fetch emails
def host = "imap.gmail.com"
def port = "993"
Properties props = new Properties()
props.setProperty("mail.store.protocol", "imap")
props.setProperty("mail.imap.host", host)
props.setProperty("mail.imap.port", port)
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
def session = Session.getDefaultInstance(props, null)
def store = session.getStore("imap")
store.connect(host, username, password)
def inbox = store.getFolder("INBOX")
inbox.open(Folder.READ_ONLY)
def messages = inbox.getMessages()
This code fails while connecting imap store.
javax.mail.MessagingException: * BYE Cannot connect to IMAP server
If I skip sending email using smtp, the fetching part works. Both sending and fetching mails uses same gmail account. I guess there can be some problem with session or gmail limits. The sending email is working as well.
I am building an android app. I want to send an email from xxxxx#outlook.com. This is the code.
public void setUp
{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
this.mailhost = "smtp.live.com";
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.starttls.enable", "true");
}
I know that user should be the whole email address. But when I used I received an email that said I should start session before send a email.
This code worked 3 times and then stopped.
What does the debug output show? What was the exact error message? Exactly what did it say in the email you received? You may need to connect with POP3 or IMAP to read mail before it will let you send mail.
You should get a session from Javamail, to create your message. Then, using a transport object, you can send it.
String host = "localhost";
int port = 443;
String user = "BruceWayne#example.org";
String password = "S3cr3tP4ss";
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user, "Dark Knight"));
msg.setSubject("Hello Selina");
msg.setText("Do you want to have diner ?");
Transport transport = session.getTransport("smtp");
transport.connect(host, port, user, password);
transport.sendMessage(msg, msg.getAllRecipients());
I wonder if it is possible to connect to Hotmail with JavaMail?
I've tried this but it doesn't work, connection refused...
String host = "pop3.live.com";
String username = "laqetqetqet#hotmail.com";
String password = "rqetqetq";
Session session;
Store store;
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");
URLName url = new URLName("pop3", host, 995, "", username, password);
session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
Anyone already succeeded to do this?
Hotmail now supports pop3 (through SSL).
Thus, you need the following settings:
pop3Props.setProperty("mail.pop3.ssl.enable",
"true");
For all other properties, you must add a "s" in the properties string (so it says "pop3s" instead of "pop3"):
pop3Props.setProperty("mail.pop3s.socketFactory.class",
SSL_FACTORY);
pop3Props.setProperty("mail.pop3s.socketFactory.fallback",
"false");
pop3Props.setProperty("mail.pop3s.port",
"995");
pop3Props.setProperty("mail.pop3s.socketFactory.port",
"995");
For me, the following code works nicely:
String host = "pop3.live.com";
String username = "laqetqetqet#hotmail.com";
String password = "rqetqetq";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3s.port", "995");
Session session = Session.getInstance(pop3Props, null);
Store store = session.getStore("pop3s");
store.connect(host, 995, username, password);
You could try this SourceForge project
MrPostman is an email gateway from local POP clients like Microsoft Outlook, Mozilla's mail client etc. to different web mail services like Yahoo and Hotmail.It is being designed for extensibility so is easy to add more web mail services to it.