0

I am new to java mail, I want to sent a mail with attachments so i like to create a test mail with sample codes from internet but i am getting

javax.mail.sendfailedexception: sending failed;
nested exception is: class javax.mail.authenticationfailedexception 
at javax.mail.transport.send0(Transport.java.218)
at javax.mail.transport.send(Transport.java.80)

I tried with different authentication but i failed

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   public static void main(String [] args)
   {

      // Recipient's email ID needs to be mentioned.
      String to = "hari@gmail.com";

      // Sender's email My Office mail server
      String from = "hari8750@access.co.in";
      String pass = "Password";
      String host = "172.23.5.10";
      String port = "25";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.setProperty("mail.smtp.port", port);
      properties.setProperty("mail.smtp.auth", "true");

      // Get the default Session object.
      Session session = Session.getInstance(properties,new MailAuthentication(from,pass));

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("Test Mail");

         // Create the message part 
         BodyPart messageBodyPart = new MimeBodyPart();

         // Fill the message
         messageBodyPart.setText("Test Mail Success Hari");

         // Create a multipar message
         Multipart multipart = new MimeMultipart();

         // Set text message part
         multipart.addBodyPart(messageBodyPart);

         // Send the complete message parts
         message.setContent(multipart );

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

import javax.mail.*
public class MailAuthentication extends Authentication
{

    String _user;
    String _pass;
    public GMailAuthenticator (String username, String password)
    {
        super();
        this._user = username;
        this._pass = password;
    }
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(_user, _pass);
    }
}

i also tried with

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

and

Session session = Session.getDefaultInstance(properties);

and

Session session = Session.getDefaultInstance(properties);

but i am getting the same error.

Kindly help me to solve this out

I am using Windows PC like java environment variables is there any steps for SMTP ??

Thanks in Advance.

Hari
  • 1
  • 1
  • 2
  • check your username and password values. are they correct? – Bilbo Baggins Jul 08 '14 at 05:38
  • Dear @SamwiseGamgee Username and password are correct i directly login to the port 172.23.5.10 (company mail server) in browser with this and outlook also configured with this IP and username ,password. – Hari Jul 08 '14 at 05:48
  • Examine the [protocol trace](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) for more clues as to why the server is rejecting your username and password. You might have to set the "mail.debug.auth" property to "true" to get the full protocol trace. Be careful not to post it here because it might expose your password. – Bill Shannon Jul 08 '14 at 06:31

2 Answers2

0

You must add mail.smtp.ssl and mail.smtp.sender.address properties.. mail.smtp.sender.address is same as from property. It must work..it is working as expected for me.. Hope it helps

If it doesn't solve your problem, just share the exception stack trace of caused by..

Mr.Chowdary
  • 3,389
  • 9
  • 42
  • 66
  • Dear @Mr.Chowdary i Modified properties as properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", port); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.ssl.enable", "true"); properties.setProperty("mail.smtp.ssl.trust", "*"); properties.setProperty("mail.smtp.sender.address", username); properties.setProperty("mail.smtp.from", username); as per your advise but not also i am getting – Hari Jul 08 '14 at 07:21
  • javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.authenticationfailedexception at javax.mail.transport.send0(Transport.java:218) at javax.mail.transport.send(Transport.java:80) at com.access.SendEmail.main(SendEmail.java:90) error. – Hari Jul 08 '14 at 07:24
  • @Hari Check it first with gmail.com by passing host as smtp.gmail.com and gmail id and password.. and check again with your specific values.. If both are not working means your code problem.. if gmail working means your input values are wrong for access.co.in mail server.. check it and let me know.. :) – Mr.Chowdary Jul 08 '14 at 09:32
-2

You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings so that your account is no longer protected by modern security standards.

honk
  • 9,137
  • 11
  • 75
  • 83
Nadav Finish
  • 1,120
  • 9
  • 7