Why timeout when sending mail via office365 smtp in java?

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
Account to Office 365. Tried to send mail by java programs that are able to find on the Internet (via smtp.office365.com). Always get timeout exception.

Do you have examples of source code that exactly work?

import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; public class SendEmailOffice365 { private static final Logger LOGGER = Logger.getAnonymousLogger(); private static final String SERVER_HOST = "smtp.office365.com"; private static final int SERVER_PORT = 587; private static final String LOGIN = "========"; private static final String PASSWORD = "========"; private static final String from = "========"; private static final String to = "========"; private static final String subject = "Test"; private static final String messageContent = "Test message"; private static void sendEmail() { final Session Session = Session.getInstance(getEmailProperties(), new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(LOGIN, PASSWORD); } }); try { final Message message = new MimeMessage(session); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setFrom(new InternetAddress(from)); message.setSubject(subject); message.setText(messageContent); message.setSentDate(new Date()); Transport.send(message); } catch (final MessagingException ex) { LOGGER.log(Level.WARNING, "Error sending message:" + ex.getMessage(), ex); } } private static Properties getEmailProperties() { final Properties config = new Properties(); config.put("mail.smtp.auth", "true"); config.put("mail.smtp.starttls.enable", "true"); config.put("mail.transport.protocol", "smtp"); config.put("mail.smtp.host", SERVER_HOST); config.put("mail.smtp.port", SERVER_PORT); config.put("mail.smtp.user", LOGIN); config.put("mail.smtp.password", PASSWORD); config.put("mail.smtp.connectiontimeout", "2000"); config.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); return config; } public static void main(final String[] args) { sendEmail(); } }
by | 209 views

1 Answer

0 like 0 dislike
And You do not mind setting the timeout to 2 seconds? I mean the line:
config.put("mail.smtp.connectiontimeout", "2000");

I think if You just salmeterola will no longer fall off by timeout.

Related questions

0 like 0 dislike
2 answers
110,608 questions
257,187 answers
40,796 users