cancel
Showing results for 
Search instead for 
Did you mean: 

Send html-Mail via Engine

Former Member
0 Kudos

Hi all,

I have the following problem/task:

In my application I generate the body for an hmtl-mail. Now I want to send this mail to different recipients, distribution lists, etc.

Does anyone has an example-coding?

I know that there is a lot of coding posted in several threads, but up to know I can't find/develop a working solution!

I tried the following coding, but I got the error:

Sending failed; nested exception is: javax.mail.SendFailedException: Invalid Addresses; nested exception is: javax.mail.SendFailedException: 550 5.7.1 Unable to relay for dominik.herz@sap.com

Properties props = new Properties();
	String host = "dewdfe10.wdf.sap.corp";
	props.put("mail.smtp.host", host);
	Session session = Session.getInstance(props, null);
	MimeMessage message = new MimeMessage(session);
	Address toAddress = new InternetAddress();
	Address ccAddress = new InternetAddress();
	Address bccAddress = new InternetAddress();
	Address fromAddress = new InternetAddress();
	
	try
	{
		
		MimeMultipart multipart = new MimeMultipart();
		BodyPart messageBodyPart = new MimeBodyPart();
	/* the following statements build up the email */
		fromAddress = new InternetAddress("test@sap.com");
		message.setFrom(fromAddress);
	
		//toAddress = new	InternetAddress(wdContext.currentContextElement().getCurrentUserId()+"@sap.com");
		toAddress = new	InternetAddress("dominik.herz@sap.com");
		message.setRecipient(Message.RecipientType.TO,toAddress);
	
		message.setSubject(eMailContent.getSubject());
	
		messageBodyPart.setText("Test");
		multipart.addBodyPart(messageBodyPart);
	
		message.setContent(multipart);
		Transport.send(message);
		}
	 
	 //exceptions part 
	catch (AddressException e)
	{
	wdComponentAPI.getMessageManager().reportWarning(e.
	getLocalizedMessage());
	e.printStackTrace();
	}
	catch (SendFailedException e)
	{
	wdComponentAPI.getMessageManager().reportWarning(e.
	getLocalizedMessage());
	e.printStackTrace();
	}
	catch (MessagingException e)
	{
	wdComponentAPI.getMessageManager().reportWarning(
	e.getLocalizedMessage());
	e.printStackTrace();
	}

Thanks and best regards,

Dominik

Accepted Solutions (0)

Answers (3)

Answers (3)

chintan_virani
Active Contributor
0 Kudos

You just need to put the correct SMTP server of your company.

If you want mails to be send out to different people you can create a class (for eg: MailSender.java) and have method sendMail which accepts FromAdress, ToAddress,Subject). Add the class file to your WD project.

Now call this method from the WD application as follows:-

String strFromAddr = <FROM ADDRESS>
String strToAddr = <TO ADDR>
String StrSubject = <SUBJECT>

MailSender mailSend = new MailSender();
mailSend.sendMail(strFromAddr,strToAddr,strSubject);

Former Member
0 Kudos

Hi!

Thanks for your ideas... I will try it!

Regards,

Dominik

Former Member
0 Kudos

Hi

This Program gives any example on how to do SMTP Authentication

(User and Password verification)

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

/* To use this program, change values for the following three constants,

SMTP_HOST_NAME -- Has your SMTP Host Name

SMTP_AUTH_USER -- Has your SMTP Authentication UserName

SMTP_AUTH_PWD -- Has your SMTP Authentication Password

Next change values for fields

emailMsgTxt -- Message Text for the Email

emailSubjectTxt -- Subject for email

emailFromAddress -- Email Address whose name will appears as "from" address

Next change value for "emailList".

This String array has List of all Email Addresses to Email Email needs to be sent to.

Next to run the program, execute it as follows,

SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();

*/

public class SendMailUsingAuthentication

{

private static final String SMTP_HOST_NAME = "myserver.smtphost.com";

private static final String SMTP_AUTH_USER = "myusername";

private static final String SMTP_AUTH_PWD = "mypwd";

private static final String emailMsgTxt = "Online Order Confirmation Message. Also include the Tracking Number.";

private static final String emailSubjectTxt = "Order Confirmation Subject";

private static final String emailFromAddress = "me@myserver.com";

// Add List of Email address to who email needs to be sent to

private static final String[] emailList = {"mark@yahoo.com", "robin@apple.com"};

public static void main(String args[]) throws Exception

{

SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();

smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);

System.out.println("Sucessfully Sent mail to All Users");

}

public void postMail( String recipients[ ], String subject,

String message , String from) throws MessagingException

{

boolean debug = false;

//Set the host smtp address

Properties props = new Properties();

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

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

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

// create a message

Message msg = new MimeMessage(session);

// set the from and to address

InternetAddress addressFrom = new InternetAddress(from);

msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];

for (int i = 0; i < recipients.length; i++)

{

addressTo<i> = new InternetAddress(recipients<i>);

}

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

// Setting the Subject and Content Type

msg.setSubject(subject);

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

Transport.send(msg);

}

/**

  • SimpleAuthenticator is used to do simple authentication

  • when the SMTP server requires it.

*/

private class SMTPAuthenticator extends javax.mail.Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

String username = SMTP_AUTH_USER;

String password = SMTP_AUTH_PWD;

return new PasswordAuthentication(username, password);

}

}

}

Regards

Ayyapparaj

Former Member
0 Kudos

Hi,

your coding means, that every user who want to send the mail needs an passwd, right?

Is it possible to get to know this passwd dynamically?

The user shall push a button "send" and then the mail should send out, without typing a wasswd in or something else...

Is this possible?

Regards,

Dominik

Former Member
0 Kudos

Hi ,

1) Please read the following article on relaying denied: http://www.hiteksoftware.com/knowledge/articles/026.htm

Most likely the issue is that your company is using an Exchange mail server which has to allow your account and/or IP address to send smtp email. You would have to contact your mail server admin for this.

Regards

Ayyapparaj

Former Member
0 Kudos

Hmmm...

That seems to be a problem, because my application shall be used from different users. Therefore I think it's impossible to get to know all IPs...

Is there an ohter possibilty to send internal emails?

Thanks and regards,

Dominik