JavaMail with MS Exchange: No authentication mechansims supported by both server and client

前端 未结 6 2117
天命终不由人
天命终不由人 2021-01-12 01:39

I\'ve been trying for days now to send mail from Grails application and unsuccessfully. I\'m using:

  • grails 1.3.7
  • mail 1.0 plugin
  • spring-secur
6条回答
  •  星月不相逢
    2021-01-12 02:11

    Well it looks that I had few problems. At first, Exchange wasn't setup correctly. And then it seems I've tried all possible configurations but the right one. This works:

    class MymailService 
    {
        boolean transactional = false
    
        public sendMessage(String to, String cc, String msgSubject, String msgText) 
        {
            String host = "mail.mailserver.com";
            String username = "myusername@mymailserver.com"; 
            String password = "xxx"; 
            String from = "myusername@mymailserver.com";
            String port = "25";
    
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port); 
            props.put("mail.smtp.auth", "false");
    
            Transport transport = null;
    
            try{
                Session session = Session.getDefaultInstance(props, null);
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
    
            InternetAddress to_address = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, to_address);
    
            InternetAddress cc_address = new InternetAddress(cc);
            message.addRecipient(Message.RecipientType.CC, cc_address);
    
            message.setSubject(msgSubject);
            message.setText(msgText);
    
            transport = session.getTransport("smtp");
            transport.connect();
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore){}
        }
    }
    }
    

    The final clue was Bill Shannon's post. Thanks Bill!

提交回复
热议问题