Announcement Announcement Module
Collapse
No announcement yet.
problem sending an email with javamail Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dr_pompeii
    started a topic problem sending an email with javamail

    problem sending an email with javamail

    Hello guys
    i need really your help, my boss have a gun and .... i want to live,
    i cant send a simple email
    this is my xml configuration

    Code:
    <beans>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
       <property name="host"> 
       	<value>mail.yahoo.com</value>
        </property>
        <property name="username" >
       	<value>usernamesenderyahoo</value> <!-- same result if i write with @yahoo.com-->
        </property>
        <property name="password">
    	 <value>passwordsenderyahoo</value>
        </property>		    	    
        <property name="javaMailProperties">
            <props>
    	  <prop key="mail.smtp.auth">true</prop>
            </props>
        </property>
    </bean>
    		
    <bean id="messageSender" class="com.test.ComplexMessageSender" >
       <property name="javaMailSender">
    	<ref local="mailSender"/>
       </property>
       <property name="to">
    	<value>[email protected]</value>
       </property>
       <property name="from">
    	<value>[email protected]</value>
       </property>
       <property name="subject">
    	<value>Spring mail</value>
       </property>						
    </bean>			  
    </beans>
    when i start up the tomcat 5.5.27, by "ant" i execute a java class that call a bean

    Code:
    ComplexMessageSender plex = (ComplexMessageSender) ctx.getBean("messageSender");			
    plex.sendMessage();
    so after almost 3-5 minutes i recieve this prize

    Code:
     [java]    [java] EXCPETIONMail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: mail.yahoo.com, port: 25;
         [java] nested exception is:
         [java] java.net.ConnectException: Connection timed out
         [java] Stack
         [java] CLASEclass org.springframework.mail.MailSendException
         [java] org.springframework.mail.MailSendException; nested exceptions (0) are:
         [java] Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mail.yahoo.com, port: 25;
         [java] nested exception is:
         [java] java.net.ConnectException: Connection timed out
         [java] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
         [java] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
         [java] at javax.mail.Service.connect(Service.java:233)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:326)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:330)
         [java] at com.test.ComplexMessageSender.sendMessage(Unknown Source)
         [java] at Manolo.pruebas(Unknown Source)
         [java] at Manolo.main(Unknown Source)
    BUILD SUCCESSFUL
    Total time: 5 minutes 17 seconds
    or the second prize is this
    Code:
     [java] EXCPETIONAuthentication failed; nested exception is javax.mail.AuthenticationFailedException
         [java] Stack
         [java] CLASEclass org.springframework.mail.MailAuthenticationException
         [java] org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException
         [java] Caused by: javax.mail.AuthenticationFailedException
         [java] at javax.mail.Service.connect(Service.java:264)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:326)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:330)
         [java] at com.test.ComplexMessageSender.sendMessage(Unknown Source)
         [java] at Manolo.pruebas(Unknown Source)
         [java] at Manolo.main(Unknown Source)
    i dont have any idea what could be wrong
    plse help!!!

    thanks for advanced
    Last edited by dr_pompeii; Jan 16th, 2007, 10:45 AM.

  • Andre.almeida
    replied
    Good morning guys,

    I'm facing a problem with my e-mail sender. I have this conf:

    Code:
            <bean id="mailSession" class="javax.mail.Session" factory-method="getInstance">
    		<constructor-arg>
    			<props>
    				<prop key="mail.smtp.auth">true</prop>
    				<prop key="mail.smtp.socketFactory.port">465</prop>
    				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
    				<prop key="mail.smto.socketFactory.fallback">false</prop>
    			</props>			
    		</constructor-arg>
    		<constructor-arg ref="smtpAutentificador"/>
    	</bean>
    	
    	<bean id="mailSender"
    		class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host" value="smtp.gmail.com" />
    		<property name="session" ref="mailSession" />
    	</bean>
    	
    	
    	<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    		<property name="from" value="[email protected]"/>
    		<property name="subject" value="Testing"/>
    	</bean>
    	
    	<bean id="email" class="com.utils.EmailServlet">
    		<constructor-arg ref="mailSender"/>
    		<constructor-arg ref="templateMessage"/>
    	</bean>
    
            <bean id="smtpAutentificador" class="com.utils.SmtpAutentificador">
    		<constructor-arg value="[email protected]"/>
    		<constructor-arg value="myPassword"/>
    	</bean>
    
            <bean id="emailServlet" class="com.utils.EmailServlet" >
    		<constructor-arg ref="mailSender" />
    		<constructor-arg ref="templateMessage" />
    	</bean>

    and the classes that I send the e-mails are just the same as posted here. When I run it in my local machine, everything works fine, but when I deploy it on my JBOSS and try to send an e-mail I get this error:

    Code:
    07:54:38,963 ERROR [[/web]] Exception while dispatching incoming RPC call
    com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String com.client.rpc.EnviarEmailServiceRpc.enviarEmail(com.model.Regra,com.model.Usuario,java.lang.String,
    com.model.Usuario,java.lang.String) throws com.exception.Exception' threw an unexpected exception: 
    java.lang.IllegalAccessError: tried to access method javax.mail.internet.MimeBodyPart.setText
    (Ljavax/mail/internet/MimePart;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V from class javax.mail.internet.MimeMessage
            at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java: 378)
            at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:581)
            at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:544)
            at org.gwtrpcspring.RemoteServiceDispatcher.invokeAndEncodeResponse(RemoteServiceDispatcher.java:80)
            at org.gwtrpcspring.RemoteServiceDispatcher.processCall(RemoteServiceDispatcher.java:53)
            at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
            at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at com.server.servlet.AuthFilter.doFilter(AuthFilter.java:35)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
     ...
    Caused by: java.lang.IllegalAccessError: tried to access method javax.mail.internet.MimeBodyPart.setText(Ljavax/mail/internet/MimePart;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V from class javax.mail.internet.MimeMessage
            at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1520)
            at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1504)
            at org.springframework.mail.javamail.MimeMessageHelper.setPlainTextToMimePart(MimeMessageHelper.java:847)
            at org.springframework.mail.javamail.MimeMessageHelper.setText(MimeMessageHelper.java:794)
            at org.springframework.mail.javamail.MimeMessageHelper.setText(MimeMessageHelper.java:767)
            at org.springframework.mail.javamail.MimeMailMessage.setText(MimeMailMessage.java:168)
            at org.springframework.mail.SimpleMailMessage.copyTo(SimpleMailMessage.java:197)
            at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:303)
            at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
    ...
    I use javax.mail-1.4.1 but when I try to use javax.mail-1.4.4 the same error occurs.
    Do you guys have any idea what's going on?

    Thanks for your help.
    Last edited by Andre.almeida; Mar 9th, 2011, 09:13 AM.

    Leave a comment:


  • arctangant
    replied
    thanks for codes , i found via google , this is my first post . have nice days

    Leave a comment:


  • jrade
    replied
    Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException

    are the jars on your classpath?

    Leave a comment:


  • SKC_CVR
    replied
    please help i did everything correctly in it i have both mail.jar and activaion.jar in the required java directory but still i'm getting this error

    23:32:40,890 ERROR org.springframework.osgi.context.support.OsgiBundl eXmlApplicationContext:307 - Post refresh error
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mailSender' defined in URL [bundleentry://4.fwk32745991/META-INF/spring/spring.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:883)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:839)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
    at org.springframework.osgi.context.support.AbstractD elegatedExecutionApplicationContext.completeRefres h(AbstractDelegatedExecutionApplicationContext.jav a:288)
    at org.springframework.osgi.extender.internal.depende ncies.startup.DependencyWaiterApplicationContextEx ecutor$CompleteRefreshTask.run(DependencyWaiterApp licationContextExecutor.java:145)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unk nown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:54)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:877)
    ... 15 more
    Exception in thread "SpringOsgiExtenderThread-36" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mailSender' defined in URL [bundleentry://4.fwk32745991/META-INF/spring/spring.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:883)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:839)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
    at org.springframework.osgi.context.support.AbstractD elegatedExecutionApplicationContext.completeRefres h(AbstractDelegatedExecutionApplicationContext.jav a:288)
    at org.springframework.osgi.extender.internal.depende ncies.startup.DependencyWaiterApplicationContextEx ecutor$CompleteRefreshTask.run(DependencyWaiterApp licationContextExecutor.java:145)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unk nown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:54)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:877)
    ... 15 more


    urgent plzz help anyone

    Leave a comment:


  • roi des tenebres
    replied
    Originally posted by ostewart View Post
    To send mail over an authenticated connection, you need to setup the Session with an Authenticator implementation. That's not provided by Spring out of the box, but you can easily wire in your own implementation. For gmail, you'd do something like this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    							http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
            <!-- our Authenticator implementation -->
    	<bean id="smtpAuthenticator"
    		class="forums.mailtest.SmtpAuthenticator">
    		<constructor-arg value="[email protected]" />
    		<constructor-arg value="password" />
    	</bean>
    
            <!-- now setup an authenticated session -->
    	<bean id="mailSession" class="javax.mail.Session"
    		factory-method="getInstance">
    		<constructor-arg>
    			<props>
    				<prop key="mail.smtp.auth">true</prop>
    				<prop key="mail.smtp.socketFactory.port">465</prop>
    				<prop key="mail.smtp.socketFactory.class">
    					javax.net.ssl.SSLSocketFactory
    				</prop>
    				<prop key="mail.smtp.socketFactory.fallback">
    					false
    				</prop>
    			</props>
    		</constructor-arg>
    		<constructor-arg ref="smtpAuthenticator" />
    	</bean>
    
            <!-- and configure the MailSender with the authenticated session -->
    	<bean id="mailSender"
    		class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host" value="smtp.gmail.com" />
    		<property name="session" ref="mailSession" />
    	</bean>
    
    	<!-- this is a template message that we can pre-load with default state -->
    	<bean id="templateMessage"
    		class="org.springframework.mail.SimpleMailMessage">
    		<property name="from" value="[email protected]" />
    		<property name="subject" value="Testing" />
    	</bean>
    
    	<bean id="mailTest" class="forums.mailtest.MailTest">
    		<constructor-arg ref="mailSender" />
    		<constructor-arg ref="templateMessage" />
    	</bean>
    </beans>
    You need your own Authenticator implementation, which could be something like this:
    Code:
    package forums.mailtest;
    
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    
    public class SmtpAuthenticator extends Authenticator {
    	private String username;
    	private String password;
    	
    	public SmtpAuthenticator(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    
    	public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
    	}
    }
    And here's the test class to send the actual message:
    Code:
    package forums.mailtest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    public class MailTest {
    	private MailSender mailSender;
    	private SimpleMailMessage templateMessage;
    	public MailTest(MailSender mailSender, SimpleMailMessage templateMessage) {
    		super();
    		this.mailSender = mailSender;
    		this.templateMessage = templateMessage;
    	}
    	
    	public void sendMeMail() {
    		SimpleMailMessage msg = new SimpleMailMessage(templateMessage);
    		msg.setTo("[email protected]");
    		msg.setText("This is a test.\nGo Spring!\n");
    		try {
    			this.mailSender.send(msg);
    		} catch (MailException e) {
    			System.err.println("Didn't work.");
    			e.printStackTrace();
    		}
    	}
    	
    	public static final void main(String[] args) {
    		ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {
    				"forums/mailtest/application-config.xml"
    		});
    		
    		MailTest tester = (MailTest) appCtx.getBean("mailTest");
    		tester.sendMeMail();
    	}
    }
    Note that you should google for the connection settings for whatever mail server you're trying to use to send mail. For gmail, they're here:
    http://mail.google.com/support/bin/a...y?answer=13287

    I think yahoo and gmail both want you to use authentication, and you may need to use a different port for each.


    please your code work but just with simplemessage , can you rewrite your code using mimemessage because I need to attach a file , and if possible make the authentification in the MailTest not defined in the application-config thaaaaaanks

    Leave a comment:


  • jrade
    replied
    Works!

    Originally posted by jrade View Post
    This does not work anymore. Once I get it working, I will post a solution. Most likely the port value has changed with gmail.

    This works, my apologies. Firewall was blocking all SMTP POP ports.

    Leave a comment:


  • jrade
    replied
    This does not work anymore. Once I get it working, I will post a solution. Most likely the port value has changed with gmail.

    Leave a comment:


  • ashleyvijay
    replied
    Connection Timeout!

    Hi Im having problems with sending out emails. Would appreciate help

    GidWS.java
    Code:
    public class GidWS {
    
    	private MailSender mailSender;
        private SimpleMailMessage templateMessage;
    
        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void setTemplateMessage(SimpleMailMessage templateMessage) {
            this.templateMessage = templateMessage;
        }
    	
        public void sendMeMail() {
        	
        	SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
            msg.setTo("[email protected]");
    		msg.setText("Testing Mail.....");
    		 try{
    			 
    	            this.mailSender.send(msg);
    	        }
    	        catch(MailException ex) {
    	           
    	            System.err.println(ex.getMessage()); 
    	            ex.printStackTrace();
    	        }
            }
    
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("gid.xml", GidWS.class);
    		String requestXml =
    				"<hr:CompanyRequest xmlns:hr=\"http://mycompany.com/hr/schemas\">" +
    				"<hr:Company>"+
    		        "<hr:CompanyName>GetAllCompanies</hr:CompanyName>"+
    	            "</hr:Company>"+
                    "</hr:CompanyRequest>";
    
    		Message<String> message = MessageBuilder.fromPayload(requestXml)
    				.setHeader(AbstractWebServiceHandler.SOAP_ACTION_PROPERTY_KEY, "http://mycompany.com/hr/schemas")
    				.build();
    
    		((MessageChannel)context.getBean("sendChannel")).send(message);
    		
    		GidWS mailtester=(GidWS)context.getBean("msgManager");
    		mailtester.sendMeMail();
    		
    }
    SmtpAuthenticator.java
    Code:
    public class SmtpAuthenticator extends Authenticator {
    	
    	private String username;
    	private String password;
    	
    	public SmtpAuthenticator(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    
    	public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
    	}
    	
    }
    gid.xml
    Code:
    <message-bus/>
    	<publish-subscribe-channel id="sendChannel"/>
    	<!-- The service activator receives from the 'sendChannel',
    	     invokes the handler, and sends the reply Message to the 'replyChannel'. -->
    	<service-activator input-channel="sendChannel" ref="msgConverter1" output-channel="replyChannel1"/>
    	<!-- The handler invokes the WebService for the given URI and returns a reply Message. -->
    	<ws-handler id="msgConverter1" uri="http://localhost:8080/Trial1/companyservice"/>
    	<!-- The response from the service is logged to the console. -->
    	<channel-adapter id="replyChannel1" target="console"/>
    	<console-target id="console"/>
    	<beans:bean id="msgManager" class="org.springframework.integration.gidb.ws.GidWS">
    		<beans:property name="mailSender" ref="mailSender"/>
    		<beans:property name="templateMessage" ref="templateMessage"/>
    	</beans:bean>
    	<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<beans:property name="host" value="66.249.93.109"/>
    		<beans:property name="session" ref="mailSession" />
    	</beans:bean>
    	
    <beans:bean id="mailSession" class="javax.mail.Session"
    		factory-method="getInstance">
    		<beans:constructor-arg>
    			<beans:props>
    				<beans:prop key="mail.smtp.auth">true</beans:prop>
    				<beans:prop key="mail.smtp.starttls.enable">true</beans:prop>
    				<beans:prop key="mail.smtp.debug">true</beans:prop>
    				<beans:prop key="mail.smtp.socketFactory.port">465</beans:prop>
    				<beans:prop key="mail.smtp.socketFactory.class">
    					javax.net.ssl.SSLSocketFactory
    				</beans:prop>
    				<beans:prop key="mail.smtp.socketFactory.fallback">
    					false
    				</beans:prop>
    			</beans:props>
    Error Message
    Code:
    nested exception is:
    	java.net.ConnectException: Connection timed out: connect
    org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: 66.249.93.109, port: 25;
      nested exception is:
    	java.net.ConnectException: Connection timed out: connect
    	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:419)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:308)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)
    	at org.springframework.integration.gidb.ws.GidWS.sendMeMail(GidWS.java:36)
    	at org.springframework.integration.gidb.ws.GidWS.main(GidWS.java:81)
    Caused by: javax.mail.MessagingException: Could not connect to SMTP host: 66.249.93.109, port: 25;
      nested exception is:
    	java.net.ConnectException: Connection timed out: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
    	at javax.mail.Service.connect(Service.java:310)
    	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    	... 4 more
    Caused by: java.net.ConnectException: Connection timed out: connect
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    	at java.net.Socket.connect(Socket.java:520)
    	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:545)
    	at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
    	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
    	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
    	... 7 more

    Leave a comment:


  • Mark80
    replied
    I'm Sorry

    I'M Sorry for my previous post....
    the configuration is perfec....
    the mail server is slow tonight!!!
    BYE BYE!!!

    Leave a comment:


  • Mark80
    replied
    Yes...all perfect but...

    Hi,
    I'm an italian boy...and i'm sorry for my english!!!
    well...
    the configuration for send email through spring whit gmail account is perfect but I send email only from my gmail address to my gmail address!!!
    do you know why ??? help me please!!!
    thanks!!!

    Leave a comment:


  • dr_pompeii
    replied
    Hello guys

    by order

    ostewart - thanks so much for your code!!!!
    well, after seen the link of our friend karldmoore, i did my own code (early morning), but tonight i conclude that your code looks much better - thanks!!!

    changing the theme, i saw this part factory-method, first time that i see that,
    well, reading here more documentation http://www.springframework.org/docs/...nce/beans.html
    the factory-method is for static methods like the getInstance of Session class
    http://java.sun.com/products/javamai...l/Session.html

    i have a simple doubt , here 3.3.1.3. Some examples, i can see the follow example
    Code:
    <bean id="exampleBean" class="examples.ExampleBean"
          factory-method="createInstance">
      <constructor-arg ref="anotherExampleBean"/>
      <constructor-arg ref="yetAnotherBean"/>
      <constructor-arg value="1"/> 
    </bean>
    
    <bean id="anotherExampleBean" class="examples.AnotherBean"/>
    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
    
    public class ExampleBean {
    ...    
        public static ExampleBean createInstance (
                AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
            ExampleBean eb = new ExampleBean (...);
            // some other operations
            ...
            return eb;
        }
    the question is if i have other static method (createInstancebeta with the sames arguments for example), how i should declare the other method in the <bean ...., because i can see that the Session class, has more static methods

    karldmoore
    Wow dr_pompeii, we found some code that works? Lets just hope they don't change anything an it carries on working! Glad its sorted anyway!
    lol, yes man, i do thinking both should buy a lotery

    really thanks so much for your time and effort guys,
    my boss sold his gun

    Leave a comment:


  • karldmoore
    replied
    Wow dr_pompeii, we found some code that works? Lets just hope they don't change anything an it carries on working! Glad its sorted anyway!

    Leave a comment:


  • ostewart
    replied
    To send mail over an authenticated connection, you need to setup the Session with an Authenticator implementation. That's not provided by Spring out of the box, but you can easily wire in your own implementation. For gmail, you'd do something like this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    							http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
            <!-- our Authenticator implementation -->
    	<bean id="smtpAuthenticator"
    		class="forums.mailtest.SmtpAuthenticator">
    		<constructor-arg value="[email protected]m" />
    		<constructor-arg value="password" />
    	</bean>
    
            <!-- now setup an authenticated session -->
    	<bean id="mailSession" class="javax.mail.Session"
    		factory-method="getInstance">
    		<constructor-arg>
    			<props>
    				<prop key="mail.smtp.auth">true</prop>
    				<prop key="mail.smtp.socketFactory.port">465</prop>
    				<prop key="mail.smtp.socketFactory.class">
    					javax.net.ssl.SSLSocketFactory
    				</prop>
    				<prop key="mail.smtp.socketFactory.fallback">
    					false
    				</prop>
    			</props>
    		</constructor-arg>
    		<constructor-arg ref="smtpAuthenticator" />
    	</bean>
    
            <!-- and configure the MailSender with the authenticated session -->
    	<bean id="mailSender"
    		class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host" value="smtp.gmail.com" />
    		<property name="session" ref="mailSession" />
    	</bean>
    
    	<!-- this is a template message that we can pre-load with default state -->
    	<bean id="templateMessage"
    		class="org.springframework.mail.SimpleMailMessage">
    		<property name="from" value="[email protected]" />
    		<property name="subject" value="Testing" />
    	</bean>
    
    	<bean id="mailTest" class="forums.mailtest.MailTest">
    		<constructor-arg ref="mailSender" />
    		<constructor-arg ref="templateMessage" />
    	</bean>
    </beans>
    You need your own Authenticator implementation, which could be something like this:
    Code:
    package forums.mailtest;
    
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    
    public class SmtpAuthenticator extends Authenticator {
    	private String username;
    	private String password;
    	
    	public SmtpAuthenticator(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    
    	public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
    	}
    }
    And here's the test class to send the actual message:
    Code:
    package forums.mailtest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    public class MailTest {
    	private MailSender mailSender;
    	private SimpleMailMessage templateMessage;
    	public MailTest(MailSender mailSender, SimpleMailMessage templateMessage) {
    		super();
    		this.mailSender = mailSender;
    		this.templateMessage = templateMessage;
    	}
    	
    	public void sendMeMail() {
    		SimpleMailMessage msg = new SimpleMailMessage(templateMessage);
    		msg.setTo("[email protected]");
    		msg.setText("This is a test.\nGo Spring!\n");
    		try {
    			this.mailSender.send(msg);
    		} catch (MailException e) {
    			System.err.println("Didn't work.");
    			e.printStackTrace();
    		}
    	}
    	
    	public static final void main(String[] args) {
    		ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {
    				"forums/mailtest/application-config.xml"
    		});
    		
    		MailTest tester = (MailTest) appCtx.getBean("mailTest");
    		tester.sendMeMail();
    	}
    }
    Note that you should google for the connection settings for whatever mail server you're trying to use to send mail. For gmail, they're here:
    http://mail.google.com/support/bin/a...y?answer=13287

    I think yahoo and gmail both want you to use authentication, and you may need to use a different port for each.

    Leave a comment:


  • dr_pompeii
    replied
    Hello karldmoore

    this is a golden link, his code works!
    jesus!!!

    i am using the 2 java classes by Pro Spring (free source code)
    the unique difference is the Authenticator

    regards

    Leave a comment:

Working...
X