Raju
Raju

Reputation: 31

exception occured while sending ,javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

This has come to only for (smtp.office365.com ) SMTP.

public int sendEmail(String fromName,String fromAddress,ArrayList toAddressList ,ArrayList ccAddressList,ArrayList bccAddressList,String subject,String message,String SmtpServerIP,String smtpUserName,String smtpPassword, ArrayList<String>  attachmentFilePath ){
        SMTP_HOST_NAME=SmtpServerIP;
        SMTP_AUTH_USER=smtpUserName;
        SMTP_AUTH_PWD=smtpPassword;
        String emailmultipart="true";
        String smtpHost=SmtpServerIP;
        //System.out.println("SmtpServerIP"+SmtpServerIP);

        System.out.println("fromName :"+fromName+":SmtpServerIP:"+SmtpServerIP+":smtpPassword:"+smtpPassword+":smtpUserName:"+smtpUserName);
        boolean debug = false;
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable","true"); 
        props.setProperty("mail.transport.protocol", "smtp");
        if(smtpHost==null){
          return -99; 
        }
        if(smtpHost.length()>0){
          //props.put("mail.smtp.host", smtpHost);
          props.setProperty("mail.host", smtpHost);
         }
        else
        { 
          return 1; 
        }//Error No SmtpHost name Found.
       /* if(smtpUserName!=null && smtpUserName.length()>0){
            props.setProperty("mail.user", smtpUserName);
        }
        if(smtpPassword!=null && smtpPassword.length()>0){
            props.setProperty("mail.password", smtpPassword);
        }*/
        Session session=null;

        if(smtpUserName!=null && smtpUserName.length()>0&&smtpPassword!=null && smtpPassword.length()>0){
            System.out.println("smtpUserName111111111111111"+smtpUserName);
            props.put("mail.smtp.auth","true");
            Authenticator auth = new SMTPAuthenticator();
            session = Session.getDefaultInstance(props, auth);
        }else{
            System.out.println("smtpUserName2222222222222222"+smtpUserName);
            session = Session.getDefaultInstance(props, null);  
        }
        session.setDebug(debug);
        try{
            //System.out.println("toAddressList.size()"+toAddressList.size());
            Message msg = new MimeMessage(session);
            InternetAddress from = new InternetAddress(fromAddress,fromName);
            String msgSubject=subject;
            msg.setFrom(from);
            msg.setSubject(msgSubject);
            msg.setContent(message, "text/html; charset=utf-8");

            //msg.setSentDate(new Date(2005,9,12));

            //for adding TO address list.
            if(toAddressList.size()>0){
              Address toAddresses[]= new InternetAddress[toAddressList.size()];      
              toAddresses=getAddresses(toAddressList);
              //System.out.println(" toAddresses |||"+toAddresses);
              msg.setRecipients(Message.RecipientType.TO,toAddresses);

             }
             else{
                 return 2;
             }
            //for adding CC address list.
            if(ccAddressList.size()>0){
              Address ccAddresses[]= new InternetAddress[ccAddressList.size()];      
              ccAddresses=getAddresses(ccAddressList);
              msg.setRecipients(Message.RecipientType.CC,ccAddresses);
            }
            //for adding BCC address list.
            if(bccAddressList.size()>0){
              Address bccAddresses[]= new InternetAddress[bccAddressList.size()];      
              bccAddresses=getAddresses(bccAddressList);
              msg.setRecipients(Message.RecipientType.BCC,bccAddresses);
            }
            if(attachmentFilePath.size()>0){
                //System.out.println("Inside File Attachment attachmentFilePath.length()"+attachmentFilePath.length());
                //File file=new File(attachmentFilePath); 
                //if(file.exists()){
                    msg=attachFileAndMessage(msg,attachmentFilePath,message);
                //}else{
                  //  System.out.println("File does Not exists.");  
                  //  return 3;
                //}
            }
           // Transport.send(msg);
            msg.saveChanges(); // implicit with send()
            Transport transport = session.getTransport("smtp");
            //System.out.println("smtpHost :: "+smtpHost+"     smtpUserName ::"+smtpUserName+"       smtpPassword ::"+smtpPassword);
            transport.connect();
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        }
       catch(MessagingException mex){
            mex.printStackTrace();
          String errmsg=mex.getMessage().toString();
          if(errmsg.indexOf("Unknown SMTP host")>-1){
            System.out.println("Invalid SMTP server entry");
            return 101;
          }else if(errmsg.indexOf("Invalid Addresses")>-1){
            System.out.println("Invalid Address entry");
            return 102;
          }              
          System.out.println("Error 1 ::"+mex.getMessage());
          mex.printStackTrace();
          return 4;
        }
        catch(Exception e){
          System.out.println("Error 2 ::"+e.getMessage());
          e.printStackTrace();
          return 5;
        }
        return 0;
    }

Upvotes: 3

Views: 7123

Answers (3)

Kirill Yunussov
Kirill Yunussov

Reputation: 2303

I got this error when using an older version of javax.mail (1.3.1). When I switched to a later version (1.4.4), the problem went away.

If you are using Eclipse, you may need to look in the build path which libraries your project is using, not just the external ones that you specified, but also the built-in ones like JavaEE5, etc. which may have an older version of this library inside of them.

You can check the exact version of the library being used by debugging on this statement in your code: Session session = Session.getInstance(...

It will not show you the source code, but will give you directory path of the mail.jar that it's using, and you can open that up to see the version (in the MANIFEST file).

Upvotes: 0

Ritzz081
Ritzz081

Reputation: 101

If the error persists even after adding the above code.Please refer to the mail.jar of the project.May be the mail.jar is corrupted for the project.change it with the latest version of mail.jar and check

Upvotes: 0

IGusev
IGusev

Reputation: 156

I think you have problem here. You auth is empty.

Authenticator auth = new SMTPAuthenticator();

Try

 Authenticator auth = new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
        }
    };

This code works for me. javax.mail v1.5

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Main {
    public static void main(String[] args) {
        final String smtpAuthUserName = "[email protected]";
        final String smtpAuthPassword = "YOUR_PASSWORD";
        String emailFrom = "EMAIL_FROM";
        String emailTo   = "EMAIL_TO";
        Authenticator authenticator = new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(smtpAuthUserName, smtpAuthPassword);
            }
        };
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", "smtp.office365.com");
        properties.setProperty("mail.smtp.port", "587");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance( properties, authenticator );
        try
        {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] to = {new InternetAddress(emailTo)};
            message.setRecipients(Message.RecipientType.TO, to);
            message.setSubject("PLACE_SUBJECT_HERE");
            message.setText("YOUR_MESSAGE_HERE");
            Transport.send(message);
        }
        catch (MessagingException exception)
        {
            exception.printStackTrace();
        }
    }
}

Upvotes: 5

Related Questions