[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
can I send an auto mail with CCE  XML
Forum Index -> Development
Author Message
kklasen

Power User

Joined: 19/11/2020 08:33:30
Messages: 66
Offline

Hello,
I’m not sure if it is a Casa problem, but I hope you can me help any way.
I have created a site with CC and now want it to send notification E-Mails to me. I have a code, but always when it is trigger, I got an Error Message:

A problem occurred when communicating between the client and server.

Details:
your seesion is not available on server side anymore.

...
 


Didn’t CC work with all Java codes?
Code:

Code:
 public class MailSender {
 
 	
 	protected Session mailSession;
 	
 	public void login(String smtpHost, String smtpPort, String username, String password) {
 		Properties props = new Properties();
 		props.put("mail.smtp.host", smtpHost);
 		props.put("mail.smtp.socketFactory.port", smtpPort);		// sichere Verbindung
 		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");	// sichere Verbindung
 		props.put("mail.smtp.auth", "true");
 		props.put("mail.smtp.port", smtpPort);	// eigentlicher E-Mail austausch
 		
 		Authenticator auth = new Authenticator() {
 			@Override
 			protected PasswordAuthentication getPasswordAuthentication() {
 				
 				return new PasswordAuthentication(username, password);
 			}
 		};
 		
 		this.mailSession = Session.getDefaultInstance(props, auth);
 		Statusbar.outputSuccess("Loged in.");
 	}
 	
 	
 				//send(Sender@Mail.Adresse, SenderName, 	EmpfängerAdressen, 		Betreff,			 Message);
 	public void send(String senderMail, String senderName, String receiverAdresses, String subject, String message ) {
 //		throws MessagingException, IllegalStateException, UnsupportedEncodingException {
 		if(mailSession == null) {
 			throw new IllegalStateException("17 message- login failed");
 		}
 		
 		MimeMessage msg = new MimeMessage(mailSession);
 		try {
 			msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
 
 		msg.addHeader("format", "flowed");
 		msg.addHeader("content-Transfer-Encoding", "8bit");
 		
 		msg.setFrom(new InternetAddress(senderMail, senderName)); 
 		msg.setReplyTo(InternetAddress.parse(senderMail, false)); // true standart E-Mail ohne umlaute und extrazeichen / strict false alle Adressen 
 		msg.setSubject(subject, "UTF-8");
 		msg.setText(message, "UTF-8");
 		msg.setSentDate(new Date());
 		
 		msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverAdresses, false)); // angabe des Empfängers als Bcc CC oder to für normal
 		
 		Statusbar.outputSuccess("Send Mail...");
 		Transport.send(msg);	// versenden der Mail
 		Statusbar.outputSuccess("Mail sent.");
 		} catch (MessagingException | UnsupportedEncodingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 	}
 }
 


Thank you for your help!
Best regards
Klas

Edit: I test a second Code and it neither not working, it is mainly the same code
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

Hi Klas,

I assume that the sending of the mail is not done within a UI-request-response-thread - but in some parallel thread.

So I assume that the message is coming from the Statusbar-output that you are doing in the program.

Regards! Björn

PS: If my assumptions are wrong: please add the full stack trace.

Björn Müller, CaptainCasa GmbH
kklasen

Power User

Joined: 19/11/2020 08:33:30
Messages: 66
Offline

Hello Björn,

thank you, for the quick answer.

I made a SendMail button “textfield” to enter the receiver address, for test purposes.
Button is bind to “onSendMailTrainAction” and text-field to “m_txtMailReceiver”

Code:
Code:
 public void onSendMailTrainAction(javax.faces.event.ActionEvent event) {
 		Statusbar.outputMessage("SendMail Class");
 		
 		try {
 		mailReceiver = m_txtMailReceiver;
 		message = "der test 17";
 		}catch(Exception e){
 			Statusbar.outputWarning("E-mail sending Error: "+ e);
 		}
 
 		String username = "Muster@gmail.com";		// Zugangsdaten codieren oder aus sicherem Zugang
 		String password = "******";					// Zugangsdaten codieren oder aus sicherem Zugang
 		String sendMail = "Muster@gmail.com";		//"Sender@Mail.Adresse"
 		
 				
 		try {
 		
 		MailSender sender = new MailSender();
 		
 		//MailSender2 sender = new MailSender2();
 			;
 		sender.login("smtp.gmail.com", "465", username, password);
 		
 //		try {
 			sender.send(sendMail, "AutoMail", mailReceiver, "Betreff 17 test", message);
 			
 			sender.send(
 					
 		} catch (IllegalStateException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 			Statusbar.outputError("sendMail Error: " + e)
 
 		}
     	  	
     	
     };
 		}
     	  	
     	
     }
 


I have no stack trace only an error message in Browser. Or is somewhere else stack trace or a log file from CCE?

Best regards
Klas

Browser message Picture:


https://ibb.co/QM8YGxR
[Thumb - Browser-error-Screenshot 2022-01-17 113539.png]
 Filename Browser-error-Screenshot 2022-01-17 113539.png [Disk] Download
 Description Browser Error
 Filesize 18 Kbytes
 Downloaded:  132 time(s)

CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

...thanks for this info - you are clearly located in a client-roudtrip (or in other words: forget my previous assumptions...)

The logs are written to:

tomcat/works/Catalina/localhost/<yourapp> by default. There are rotating logs so pick the log with the newest date...

Could you check there?

Thanks! - Björn

PS: in the logging configuration (=> can be conifgured in tools in project configuration ==> Logging) you can set the log to output to console directly. For development this is often the nicest option.

Björn Müller, CaptainCasa GmbH
kklasen

Power User

Joined: 19/11/2020 08:33:30
Messages: 66
Offline

Hello Björn,

Here the Tomcat log. Thank you very much.

Best regards
Klas


stackTrace.txt
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

OK, so the relevant stacktrace is:


Code:
 Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
                                               	at java.net.URLClassLoader.findClass(Unknown Source)
                                               	at java.lang.ClassLoader.loadClass(Unknown Source)
                                               	at java.lang.ClassLoader.loadClass(Unknown Source)
                                               	... 50 more
 


If running project without Maven: add all .jar files that you added (esp. the one containing the mail-processing) to <project>/webcontent/WEB-INF/lib. Then reload the server, so that the .jar file(s) are located in tomcat/webapps/<yourapp>/WEB-INF/lib.

Then there are better chances! ;-)

If using Maven: rerun Maven ("clean package"), then reload in CaptainCasa toolset. Check that your .jar files of the target/directory are correclty transferred to the tomcat/webapps/<yourapp>/WEB-INF/lib directory.

Hope this helps!

Björn

Björn Müller, CaptainCasa GmbH
kklasen

Power User

Joined: 19/11/2020 08:33:30
Messages: 66
Offline

Hello Björn,

thank you for the good advice!
Without the tomcat log I never find it out that:
I have to change the jar from javax.mail-api to com.sun.mail/javax.mail-xx.jar and one line in the authorization code, from Session.getDefaultInstance(x,x) to Session.getInstance(x, x).

Just in case someone have the same problem.

Best regards

Klas
 
Forum Index -> Development
Go to:   
Powered by JForum 2.1.6 © JForum Team