0

i am creating a simple java mail program,the program is working ok and the last system print also working .but the problem is i dint received the mail in outlook.here i am using the company outlook.please some one help me. i am attaching my code here

enter code here

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




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

    {

        String host = "compny host";
        String from = "mail id";
        String to = "usr@some.com";
        String subject = "birthday mail";
        String messageText = "I am sending a message using the"
                + " simple.\n" + "happy birthday.";
        boolean sessionDebug = false;
        Properties props = System.getProperties();
        props.put("compny host", host);
        props.put("mail.smtp.port", "25");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getDefaultInstance(props, null);
        // Set debug on the Session so we can see what is going on
        // Passing false will not echo debug info, and passing true
        // will.
        session.setDebug(sessionDebug);
        try 
        {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = { new InternetAddress(to) };
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);

            Transport.send(msg);
            System.out.println("Sent message successfully....");
        } 

        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }
    }
}



output
Sent message successfully....
Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36
fAZ
  • 21
  • 1

2 Answers2

1

"Compny host" doesn't seem like correct host. Check out this tutorial http://www.tutorialspoint.com/java/java_sending_email.htm and here you have also a few examples of sending emails in Java Send email using java

Community
  • 1
  • 1
Kamila O
  • 300
  • 2
  • 8
0

I do expect that you are using the correct host on your side.

But you are missing Username and Password.

transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);
transport.sendMessage(message, message.getAllRecipients());

or you can use the Authenticator:

Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });
tak3shi
  • 2,305
  • 1
  • 20
  • 33