cancel
Showing results for 
Search instead for 
Did you mean: 

SMPTP authentication fails

Former Member
0 Kudos
132

Hi experts!!

I want to sedn some emails from java code.

I use the following code my exception is Authentication failed. Anywhere i've looked i've seen similar code.What am i missing????

Please help!!!

Thank u in advance!!

 
	try 
	{
		
	
		
		Properties props = new Properties();
		String host = "ers.ebeth.gr";
		props.put("mail.smtp.host", host);
		props.setProperty("mail.smtp.port" , "25");
		props.setProperty("mail.smtp.auth" , "true");
		Authenticator authenticator = new Authenticator();
		props.put("mail.transport.protocol", "smtp");
	//	props.setProperty("mail.smtp.user", "backoffice");
	//	props.setProperty("mail.smtp.password", "bckffice");
		Session session = Session.getInstance(props, null);
		MimeMessage message = new MimeMessage(session);
		Address toAddress = new InternetAddress();
		Address fromAddress = new InternetAddress();
		Address ccAddress = new InternetAddress();
		Address bccAddress = new InternetAddress();

		
		
		
		
		
		MimeMultipart multipart = new MimeMultipart();
		
		BodyPart messageBodyPart = new MimeBodyPart();
		if (! wdContext.currentEmailElement().getFrom().equals(""))
		{
			fromAddress = new InternetAddress(wdContext.currentEmailElement().getFrom());			 
			message.setFrom(fromAddress);
		}
		if (! wdContext.currentEmailElement().getTo().equals(""))
		{
			
			toAddress = new InternetAddress(wdContext.currentEmailElement().getTo());
			message.setRecipient(Message.RecipientType.TO, toAddress);
		}
		//if (! wdContext.currentEmailElement().getCc().equals(""))
		//{
		//	ccAddress = new InternetAddress(wdContext.currentEmailElement().getCc());
		//	message.setRecipient(Message.RecipientType.CC, ccAddress);
		//}
		//if (! wdContext.currentEmailElement().getBcc().equals(""))
		//{
		//	bccAddress = new InternetAddress(wdContext.currentEmailElement().getBcc());
		//	message.setRecipient(Message.RecipientType.BCC, bccAddress);
		//}
		if (! wdContext.currentEmailElement().getSubject().equals(""))
		{
			message.setSubject(wdContext.currentEmailElement().getSubject());
		}	
			
	    		
		if (! wdContext.currentEmailElement().getBody().equals("") )
		{
			messageBodyPart.setText(wdContext.currentEmailElement().getBody()+ "u0395u039Cu03A0u039Fu03A1u0399u039Au039F u039Au0391u0399 u0392u0399u039Fu039Cu0397u03A7u0391u039Du0399u039Au039F u0395u03A0u0399u039Cu0395u039Bu0397u03A4u0397u03A1u0399u039F u0398u0395u03A3u03A3u0391u039Bu039Fu039Du0399u039Au0397u03A3, u03A4u03C3u03B9u03BCu03B9u03C3u03BAu03AE 29, 546 24 u0398u03B5u03C3u03C3u03B1u03BBu03BFu03BDu03AFu03BAu03B7, u03C4u03B7u03BB. 2310370151, Fax: 2310370166");
		}	 
	   multipart.addBodyPart(messageBodyPart);
		
	/*	messageBodyPart = new MimeBodyPart();
		String filename = "temp\\webdynpro\\web\\local\\TutWD_EmailInteractiveForm\\Components\\com.sap.tut.wd.emailinteractiveform.EmailInteractiveFormComp\\TravelRequest.pdf";
		DataSource source = new FileDataSource(filename);
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName(source.getName());				
		messageBodyPart.setHeader("Content-Type","application/pdf");
		multipart.addBodyPart(messageBodyPart);
	*/

		message.setContent(multipart);  
		Transport.send(message);

		wdComponentAPI.getMessageManager().reportSuccess("H u0391u03A0u039Fu03A3u03A4u039Fu039Bu0397 u03A0u03A1u0391u0393u039Cu0391u03A4u039Fu03A0u039Fu0399u0397u0398u0397u039Au0395 u0395u03A0u0399u03A4u03A5u03A7u03A9u03A3!");
	} 
	catch (AddressException e) 
	{
		wdComponentAPI.getMessageManager().reportSuccess(e.getLocalizedMessage());
		
	} 
	catch (SendFailedException e) 
	{
		//wdComponentAPI.getMessageManager().reportSuccess(e.getLocalizedMessage());
		wdComponentAPI.getMessageManager().reportSuccess(e.getLocalizedMessage());
		//e.printStackTrace();
	} 
	catch (MessagingException e) 
	{
		wdComponentAPI.getMessageManager().reportWarning(e.getLocalizedMessage());
		//e.printStackTrace();
	}


// and this is my Authenticator class:


   	private class Authenticator extends javax.mail.Authenticator {
 		private PasswordAuthentication authentication;
 
 		public Authenticator() {
			String username = "backoffice";  //auth-user
  			String password = "bckffice";   //auth-password
  			authentication = new PasswordAuthentication(username, password);
			
  		}
  
  		protected PasswordAuthentication getPasswordAuthentication() {
  			return authentication;
 		}
 		

  	}


    

Edited by: Grigoria Koutsogianni on Dec 4, 2008 3:36 PM

View Entire Topic
Former Member
0 Kudos

Giassu Grigoria,

Interesting. You have implemented the Authenticator, you have instantiated the object, but you have never used it!!!

The javax.mail.Authenticator is supposed to be provided to the SMTP session object (javax.mail.Session). Then the session is wrapped inside the javax.mail.Message as a constuctor parameter.

You can see a good Java Mail API example [here|http://snippets.dzone.com/posts/show/3328].

Good luck!

Tsvetomir