IMAP connection error with gmail just after using SMTP - java

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.

Related

Why I cannot use Gmail configuration for Zimbra mail?

I have gmail configuration script for send email using Gmail mail service, and these codes can send email Correctly:
public static void sendEmail() throws AddressException, MessagingException {
String emailBody = "This is body of message xxx";
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.port", "587");
mailServerProperties.put("mail.smtp.auth", "true");
mailServerProperties.put("mail.smtp.starttls.enable", "true");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(receiveremail#gmail.com));
generateMailMessage.setSubject("This is subject xxx");
generateMailMessage.setContent(emailBody, "text/html");
Transport transport = getMailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", "my-email#gmail.com", "mypasswordxxx");
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
Then I want to use Zimbra mail service with my own mail domain (findtaxi.com).
I want send email using my email domain (justin#findtaxi.com), it use Zimbra service. I follow this configuration, I change some values :
mailServerProperties.put("mail.smtp.port", "25");
transport.connect("zimbra.findtaxi.com", "justin#findtaxi.com", "thispassword");
But cannot send the message.
javax.mail.MessagingException: Could not connect to SMTP host: zimbra.findtaxi.com, port: 25; nested exception is:
java.net.ConnectException: Connection timed out: connect
Is it possible to send email with Zimbra on javamail?

Authentication Failed for iCloud IMAP Connection using Javamail

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>);

How to send an email using java mail from #outlook.com?

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());

Connect to hotmail with javamail?

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.

Using Javamail to connect to Gmail smtp server ignores specified port and tries to use 25

I'm trying to use javamail in a groovy script to send out an email via gmail. I've looked many places online and have been unable to get it working thus far. The error I'm getting when running my script is:
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?))
It appears to be trying to use port 25 even though I've specified that it should use port 587. Does anyone know what could be causing this problem, I've used telnet to connect to the smtp server on port 587, and thunderbird uses port 587 with STARTTLS security and is able to successfully send mail using the smtp server. This tells me that it is not a blocked port or connectivity issue. Here is the code I'm using to try and send the email:
import javax.mail.*
import javax.mail.internet.*
private class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication('email#gmail.com', 'password');
}
}
def d_email = "email#gmail.com",
d_password = "password",
d_host = "smtp.gmail.com",
d_port = "587", //465,587
m_to = "email#gmail.com",
m_subject = "Testing",
m_text = "This is a test."
def props = new Properties()
props.put("mail.smtp.user", d_email)
props.put("mail.smtp.host", d_host)
props.put("mail.smtp.port", d_port)
props.put("mail.smtp.starttls.enable","true")
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.socketFactory.port", d_port)
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.socketFactory.fallback", "false")
def auth = new SMTPAuthenticator()
def session = Session.getInstance(props, auth)
session.setDebug(true);
def msg = new MimeMessage(session)
msg.setText(m_text)
msg.setSubject(m_subject)
msg.setFrom(new InternetAddress(d_email))
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to))
Transport.send(msg)
Any help would be greatly appreciated. Thanks in advance!
-Bryan
In Java you would do something similar to:
Transport transport = session.getTransport("smtps");
transport.connect (smtp_host, smtp_port, smtp_username, smtp_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
Note 'smtpS' protocol. Also socketFactory properties is no longer necessary in modern JVMs but you might need to set 'mail.smtps.auth' and 'mail.smtps.starttls.enable' to 'true' for Gmail. 'mail.smtps.debug' could be helpful too.
For anyone looking for a full solution, I got this working with the following code based on maximdim's answer:
import javax.mail.*
import javax.mail.internet.*
private class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication('email#gmail.com', 'test1234');
}
}
def d_email = "email#gmail.com",
d_uname = "email",
d_password = "password",
d_host = "smtp.gmail.com",
d_port = "465", //465,587
m_to = "testepscript#gmail.com",
m_subject = "Testing",
m_text = "Hey, this is the testing email."
def props = new Properties()
props.put("mail.smtp.user", d_email)
props.put("mail.smtp.host", d_host)
props.put("mail.smtp.port", d_port)
props.put("mail.smtp.starttls.enable","true")
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.socketFactory.port", d_port)
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.socketFactory.fallback", "false")
def auth = new SMTPAuthenticator()
def session = Session.getInstance(props, auth)
session.setDebug(true);
def msg = new MimeMessage(session)
msg.setText(m_text)
msg.setSubject(m_subject)
msg.setFrom(new InternetAddress(d_email))
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to))
Transport transport = session.getTransport("smtps");
transport.connect(d_host, 465, d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
Maybe useful for anyone else running into this issue: When setting the port on the properties:
props.put("mail.smtp.port", smtpPort);
..make sure to use a string object. Using a numeric (ie Long) object will cause this statement to seemingly have no effect.

Categories