cancel
Showing results for 
Search instead for 
Did you mean: 

Please check the code reg mail implementation and give guide lines to me

venkatakalyan_karanam
Active Contributor
0 Kudos

Hi experts

I written some code for implementing the mail in webdynpro using java mail API,i did not get the out put ,Is any configuration i have to do in webdynpro or WAS

Can any body review my code and tell me where i mistaken in the implementation .Is there any thing i want to configure .Here with i am sending my code in the action of Send button

public void onActionSendMail(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSendMail(ServerEvent)

// get the values entered by the user in the form

try{

String toAddress=wdContext.currentContextElement().getTo();

String ccAddress=wdContext.currentContextElement().getCC();

String bccAddress=wdContext.currentContextElement().getBCC();

String subject=wdContext.currentContextElement().getSubject();

String messageBody=wdContext.currentContextElement().getMessage();

//set the properties of host and port no

Properties p = new Properties();

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

p.put("mail.smtp.host","12.38.145.108");

p.put("mail.smtp.port","25");

//get the session object or connection to the mail server or host

Session sess=Session.getDefaultInstance(p,null);

//create a MimeMessage and add recipients

MimeMessage message = new MimeMessage(sess);

if(toAddress !=null && toAddress.length()>0){

message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));

}else

{

wdComponentAPI.getMessageManager().reportSuccess("Please Enter the To Address");

}

if(bccAddress !=null && bccAddress.length()>0)

message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bccAddress));

if(ccAddress !=null && ccAddress.length()>0)

message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccAddress));

if(subject!=null && subject.length()>0)

message.setSubject(subject);

message.setFrom(new InternetAddress("venkatakar@intelligroup.com"));

if((messageBody!=null) && (messageBody.length()>0))

{

message.setContent(messageBody,"text/plain");

}else

{

message.setContent("","text/plain");

}

Transport.send(message);

}

catch(Exception e)

{

e.printStackTrace();

}

Please mail to : venkatakalyan.k@gmail.com

Thanks and regards

kalyan

Accepted Solutions (0)

Answers (4)

Answers (4)

venkatakalyan_karanam
Active Contributor
0 Kudos

Hi all

Thank you very much

regards

kalyan

former_member189631
Active Contributor
0 Kudos

Hi Venjkatakalyan,

Please check this,

1)Ensure your smtp host and port is correct.

Regards,

Ramganesan K.

Abhinav_Sharma
Contributor
0 Kudos

Hi Venkat,

The code seems ok to me. you don't need to configure WAS to use JavaMail APIs. However, if this code doesnot work then check with the code given below as this is working fine.

Properties prop = new Properties();

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

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

Session session = Session.getInstance(prop, null);

session.setDebug(true);

try {

MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));

InternetAddress[] address =

{ new InternetAddress(to1)};

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

msg.setSubject(subject);

msg.setSentDate(new Date());

MimeBodyPart msg1Body = new MimeBodyPart();

msg1Body.setText(msg1);

MimeBodyPart msg2Body = new MimeBodyPart();

msg2Body.setText(msg2);

Multipart mp = new MimeMultipart();

mp.addBodyPart(msg1Body);

mp.addBodyPart(msg2Body);

msg.setContent(mp);

Transport.send(msg);

} catch (AddressException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.print("AddressException : " + e.getMessage());

} catch (MessagingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.print("MessagingException : " + e.getMessage());

} catch (Exception e) {

System.out.print("Exception : " + e.getMessage());

}

where host is 12.38.145.108 in your case. May be you chk if your mail server is using another port. 25 is the default port for the smtp. This may be the case that your code is not working.

Regards:

Abhinav Sharma

PS : Do reward points if it helps

abhijeet_mukkawar
Active Contributor
0 Kudos

hi

check out this code

InitialContext ctx = new InitialContext();

Properties props = new Properties();

props.put("mail.smtp.host",<IP addres>);

Session sess = Session.getInstance(props);

// Create new mime message object

Message message = new MimeMessage(sess);

message.setFrom(new InternetAddress(emailele.getCtx_va_from()));

String recepient = emailele.getCtx_va_to();

message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));

String subject = emailele.getCtx_va_subject();

message.setSubject(subject);

String content = emailele.getCtx_va_content();

message.setContent(content,"text/plain");

Transport.send(message);

regards,

abhijeet