0
votes
import java.util.Properties; 
import javax.mail.Message;
import javax.mail.MessagingException; 
import javax.mail.Session;
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ExchangeSend {

    public static void main(String[] args) throws MessagingException {

        String to = "[email protected]";
        String cc = "[email protected]";
        String msgSubject = "Test";

        String msgText = "<i>Greetings!</i><br>";
        msgText += "<b>Wish you a nice day!</b><br>";
        msgText += "<font color=red>Duke</font>";

        //String msgText = "Automated test message from Enlite Admin. Message Succeeded";

        ExchangeSend sendM = new ExchangeSend();
        sendM.sendMessage(to, cc, msgSubject, msgText);
    }

    boolean transactional = false;

    public void sendMessage(String to, String cc, String msgSubject, String msgText) throws MessagingException {
        String host = "mail.red.com";
        String username = "e.red";
        String password = "red1";
        String from = "[email protected]";
        String port = "25";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");

        Transport transport = null;

        try {
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setContent(message, "text/html; chartset=utf-8");

            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) {
                }
            }
        }
    }
}

From the above which I posted i am able to send the text message with smtp false but when I turn smtp auth true, I get this error:

Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:829) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730) at javax.mail.Service.connect(Service.java:388)

1

1 Answers

0
votes

Your server probably wants you to make a secure connection before sending your password. Set mail.smtp.ssl.enable to true. If that doesn't work, post the JavaMail debug output.

Also, see this list of common JavaMail mistakes.