cancel
Showing results for 
Search instead for 
Did you mean: 

Sending mail in a WD application

Former Member
0 Kudos

Dear Friends,

I am trying to send a mail using the following code but i am facing errors using the Session property .Can u pls help me in sorting this out wether i should import some Jar files or anything else.I am using NWDS 7.0.1 SP3 and JDK 1.5.0_11

I am not able to resolve the type for Session and Mimemessage.Do i have to import any jar files

String host =" "; // Specify the host

// Get the From Address

String from = wdContext.currentContextElement().getFromAddress();

// Get the To Address

String to = wdContext.currentContextElement().getToAddress();

// Initialize Session

Properties props = System.getProperties();

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

*Session session = Session.getDefaultInstance(props, null);*--- //This is where i get an error//

// Create new MimeMessage

MimeMessage message = new MimeMessage(session);-----//This is where i get an error//

try {

// Set the From Address

message.setFrom(new InternetAddress(from));

// Set the To Address

message.addRecipient(Message.RecipientType.TO,

new InternetAddress(to));

// Set the Subject

message.setSubject("Test Mail");

// Set the Text

message.setText(wdContext.currentContextElement().getTextMessage());

// Send message

Transport.send(message);

messageMgr.reportSuccess("Mail Sent Successfully");

Pls suggest at the earliest.

Sunny

Edited by: Sunny.SAP on Mar 10, 2010 6:07 PM

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

I am using "mail" and "activation" used DCs.

My code for sending emails is below:


// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtpapp");
props.put("mail.smtp.port", "25");

Session session = Session.getInstance(props, null);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("sender @ domain . com"));
InternetAddress[] address = { new InternetAddress(recipient)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(title);

Multipart mp = new MimeMultipart();

// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body, "WINDOWS-1250");
mp.addBodyPart(mbp1);

msg.setContent(mp);
msg.setSentDate(new Date(System.currentTimeMillis()));
Transport.send(msg);

Regards,

--

Przemysław

Former Member
0 Kudos

The Session error was resolved after i included the external jars in my Proj- activation.jar, mailapi 1.3.3.jar

Another doubt being that is the" host" that you get from the message server host in NWDS-Window-Preferences-SAP J2ee engine-Message ServerHost ??

Thanks

Sunny

Edited by: Sunny.SAP on Mar 11, 2010 10:47 AM

Former Member
0 Kudos

Hi,

host given in NWDS is used to deploy objects - locally on your computer, and this is message server.

I think you should set value of host manually - mail server host might be different (but also could be the same, as application server).

Regards,

--

Przemysław

p330068
Active Contributor
0 Kudos

Hi Sunny,

You need to provide SMTP mail server host name and port number in the properties host and post.

Regards,

Arun

p330068
Active Contributor
0 Kudos

Hi Sunny,

Create Authenticator object and pass in the session object.

Authenticator auth = new Auth(); BEFORE your session object!

Then create JAVA class and put in your package folder. Like

package com.email;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class Auth extends Authenticator {

public PasswordAuthentication getPasswordAuthentication()

{

String username = "";

String password = "";

return new PasswordAuthentication(username,password);

}

}

Hope this will help you.

Regards,

Arun