发送office365邮件有的时候失败
如题,用下面的代码发邮件有的时候成功,有的时候失败,,,求解答。。
import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailService { static int port = 587; //smtp端口 static String server = "smtp.office365.com"; //smtp服务器地址 static String from = "xxx"; //发送者 static String user = "xxx"; //发送者地址 static String password = "ppp"; //密码 public static void sendEmail(String email, String subject, String body) { try { Properties props = new Properties(); props.put("mail.smtp.host", server); props.put("mail.smtp.port", String.valueOf(port)); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Transport transport = null; Session session = Session.getDefaultInstance(props, null); transport = session.getTransport("smtp"); transport.connect(server, user, password); MimeMessage msg = new MimeMessage(session); msg.setSentDate(new Date()); InternetAddress fromAddress = new InternetAddress(from); msg.setFrom(fromAddress); InternetAddress[] toAddress = new InternetAddress[1]; toAddress[0] = new InternetAddress(email); msg.setRecipients(Message.RecipientType.TO, toAddress); msg.setSubject(subject, "UTF-8"); msg.setText(body, "UTF-8"); msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("successful"); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } }
javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重连啊, 还能怎么办?
回复
@zp-wmhx : 这样不太好,时间太久了。
引用来自“zp-wmhx”的评论
这错误看不懂?
这错误看不懂?