1
votes

I trying to send mail from domain but getting some error.

Code:

package SendingClass;

import java.util.*;  

import java.util.Date;
import java.util.Properties; 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail  
{  
public static void main(String [] args)
{
    String host="chnmail.hcl.com";
    final String user="[email protected]";
    final String password="*******";
    String to="[email protected]";

    //Get the session object  
    Properties props = new Properties();  
    props.put("mail.smtp.host",host);  
    props.put("mail.smtp.auth", "true");  


    //new
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() 
    {  
            protected PasswordAuthentication getPasswordAuthentication() 
            {  
                return new PasswordAuthentication(user,password);  
            }  
    });  


     //Compose the message  
    try 
    {  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.setSubject("Praise the Lord");
        message.setText("This is simple program of sending email using JavaMail API");

        //send the message  
         Transport.send(message);  
         System.out.println("message sent successfully...");  
    } 
    catch (MessagingException e) 
    {
        e.printStackTrace();
    }  
}
}  

Error:

javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at SendingClass.SendEmail.main(SendEmail.java:63)
2
is the authentication mechanism supported by both client and server ? try to use the connect method without using the username and password - Pievis

2 Answers

1
votes

Most likely you need to enable SSL/TLS before your server will allow you to login. Set the property mail.smtp.ssl.enable or mail.smtp.starttls.enable to true, depending on the requirements of your server.

Also, you'll want to clean up some of these common JavaMail mistakes.

-1
votes

Does this perform any better? This is a simplified example from one of my Java application, hope I did not introduce a copypaste syntax errors. It looks similar to your code but have few extra smtp props.

Properties prop = new Properties();
prop.put("mail.smtp.allow8bitmime", "true");
prop.put("mail.smtp.timeout", "60000");
prop.put("mail.smtp.connectiontimeout", "60000");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.from", "[email protected]");

final String username = "myname";
final String pwd      = "mypwd";
Session session = Session.getInstance(prop,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, pwd);
    }
  }
);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress( (String)prop.get("mail.smtp.from") ));
((MimeMessage)msg).setSubject("My email title ÅÄÖ", "UTF-8");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]", false));

Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("This is a body text ÅÄÖ", "UTF-8");
mp.addBodyPart(mbp1);
msg.setContent(mp);

Transport tr = session.getTransport("smtp");
tr.connect();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();