Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how do i send an email using java code?

2985623Feb 2 2016 — edited Feb 2 2016

hi all,

i have been trying to send an email using a java funstion. below is my code.

im using oracle server host and port no 80

code:

"

  public static void main(String [] args)

   {   

      // Recipient's email ID needs to be mentioned.

      String to = "spriha.awin.pushpam@oracle.com";

      // Sender's email ID needs to be mentioned

      String from = "spriha.awin.pushpam@oracle.com";

      // Assuming you are sending email from localhost

      String host = "stbeehive.oracle.com";

      // Get system properties

      Properties properties = System.getProperties();

      // Setup mail server

      properties.setProperty("mail.smtp.host", host);

      properties.setProperty("mail.smtp.port", "80");

      properties.setProperty("mail.smtp.connectiontimeout", "100000");

      properties.setProperty("mail.smtp.timeout", "1000");

       System.out.println("email.Email.main()       "+properties.getProperty("mail.smtp.port"));

      // Get the default Session object.

      Session session = Session.getDefaultInstance(properties);

      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("This is the Subject Line!");

         // Now set the actual message

         message.setText("This is actual message");

          System.out.println("email.Email.main()        "+message.getSubject());

         // Send message

         Transport.send(message);

         System.out.println("Sent message successfully....");

      }catch (MessagingException mex) {

         mex.printStackTrace();

      }

   }

"

it stops at transport.send(message);

please help

thank you

spriha

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 1 2016
Added on Feb 2 2016
1 comment
533 views