on 2011 Aug 22 7:42 AM
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
Hi Shobha,
There is a spelling mistake.
Session session1 = Session.getDefaultInstance(properties, null);
Note the Upper Case 'S' in session.
Thanks
Devesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
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
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
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
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
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!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.