cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with JAVA Mail API

Former Member
0 Kudos

Hi

We have a requirement to create a custom e mail. For the same I am trying to use Java Mail API.I am facing an issue with the following code:

session session1 = session.getInstance(properties, null);

System gives an error Sourced file: inline evaluation of: ``Properties props = new Properties(); session session1 = session.getInstance(prop . . . '' : Typed variable declaration : Class: session not found in namespace

Is there some specific API i need to import for session class. Kindly suggest.

Regards

Shobha

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Shobha,

There is a spelling mistake.

 Session session1 = Session.getDefaultInstance(properties, null); 

Note the Upper Case 'S' in session.

Thanks

Devesh

Former Member
0 Kudos

Hi Devesh

Thanks for your reply! I had tried with uppercase S as well. Is didn't work.

Is there some precondition to use Java mail API?

Regards

Shobha

Former Member
0 Kudos

Hi Shobha,

The only pre-condition is imports of the necessary APIs.

You can try to have these imports to avoid any further problems


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*; 
import java.io.File;
import javax.activation.*;
import java.net.*;

Thanks

Devesh

Former Member
0 Kudos

Hi Devesh

Thanks for your inputs!

Now the privious error is resolved but I am getting an issue with Transport method.I have created a MIME message and after that I have used the following code to transport it:

Transport.send(message);

But I am getting the below mentioned error:

Error executing script: Sourced file: inline evaluation of: ``import javax.mail.; import javax.mail.internet.; import java.util.*; import j . . . '' : Method Invocation Transport.send.

Can you plz help me resolve this? Is my usage of transport.send method correct?

Regards

Shobha

Former Member
0 Kudos

Hi,

we also use this and we have something as follow:

Properties props = System.getProperties();

props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.host", mailHost);

props.put("mail.debug", "false");

props.put("mail.smtp.auth", "false");

props.put("mail.user",mailUser);

props.put("mail.from",fromUser);

Session mailSession = Session.getDefaultInstance(props);

Message msg = new MimeMessage(mailSession);

................

Transport.send(msg);

Have you ever looked at 'com.frictionless.api.util.NotificationUtil? Never got it to work but it seems as if you can also send emails with this class....

Kr,

Bram

Former Member
0 Kudos

Hi Bram

Thanks for your inputs!

I understand mailhost I can pick from system properties but where do I get MailUser and FromUser. Would this be the userID of the sender and recipient that I can pick from useraccount or something else?

Regards

Shobha

Former Member
0 Kudos

Hi Shobha,

You can use the Company document's admin email address as the From address, or use the initiator's email address from their user account.

-Howie

Former Member
0 Kudos

Thanks Howie

Even after using the new format suggested by Bram I am getting the same error with Transport.send() method. Is there some Java API we need to include to make the transport method work? Error suggests that system is not able to pick up the method in existing API classes

Regards

Shobha

Former Member
0 Kudos

Hi Shobha,

Check the log file for details of the error. It will help you in pin-pointing the exact cause of the error. If you find it cumbersome to access and scrutinise the log file, temporarily you can surround the code in a try catch block, catch the exception and dump the stack trace on the screen.

Hope this helps

Regards,

Immanuel

Former Member
0 Kudos

Hi Immanuel

Thanks for your replu!

I have looked through the log file and I found the below mentioned exception:

Target exception: javax.mail.SendFailedException: No recipient addresses

Initially I thought props.put("mail.user",mailUser) would take care of the recipient address but that doesn't seem to be the case

I tried providing recipient address explicitly using the following code:

*Address toAddress = new InternetAddress("EMailID", "UserName");

msg.addRecipient(msg.RecipientType.TO, toAddress);*

but this again threw error

Sourced file: inline evaluation of: ``import javax.mail.; import javax.mail.internet.; import java.util.; import j . . . '' : Cannot access field: RecipientType, on object: javax.mail.internet.MimeMessage@17273ed*

How shall I add recipient to this mail? Any idea?

Regards

Shobha

Former Member
0 Kudos

Here is an example of how to set the from and to addresses.

InternetAddress addressFrom = new InternetAddress(emailFrom);//emailFrom is a string containing the email address

msg.setFrom(addressFrom);

//For 2 recipients

InternetAddress[] addressTo = new InternetAddress[2];

addressTo[0] = new InternetAddress(email1);

addressTo[1] = new InternetAddress(email2);//email1 and email2 are strings containing email addresses.

msg.setRecipients(Message.RecipientType.TO, addressTo);

Please note: addressTo is an array of internet addresses.

Hope this helps.

Regards,

Immanuel

Former Member
0 Kudos

Hi Immanuel

It seems I am back to square 1

Now the exception that is coming with Transport.send() message is

  • javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25*

Any idea how to resolve this

Regards

Shobha

Former Member
0 Kudos

Hi Shobha,

check whether the SMTP details are configured properly in your system.

Configure the system property messaging.smtp.mailhost with the appropriate host ID. Talk to your Basis consultant or with the communications Divison to get clarity on the SMTP server that you should be using.

Regards,

Immanuel

Former Member
0 Kudos

Hi Shobha,

I was also facing the same issue from last couple of weeks and just now i have achieved the working functionality.

Please find below working code and replace values as per your serveru2019s configuration.

import com.sap.odp.api.util.*;

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

import java.io.File;

import java.net.*;

// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!

String to =<email address>;

String from =<email address>;

// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!

String host = <smtp host name>;

String user = <smtp user name>;

// Create properties, get Session

// Properties props = new Properties();

Properties props = System.getProperties();

props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.host", host);

props.put("mail.debug", "false");

props.put("mail.smtp.auth", "false");

props.put("mail.user",user);

props.put("mail.from",from);

Session d_session = Session.getInstance(props,null);//Authenticator object need to be set

Message msg = new MimeMessage(d_session);

msg.setFrom(new InternetAddress(from));

InternetAddress[] address = {new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject("Test E-Mail through Java");

msg.setSentDate(new Date());

msg.setText("This is a test of sending a " +

"plain text e-mail through Java.\n" +

"Here is line 2.");

Transport.send(msg);

Deepak!!!

Answers (0)