Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: problem sending an email with javamail

  1. #1
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default 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>emailaddressdestination@hotmail.com</value>
       </property>
       <property name="from">
    	<value>usernamesenderyahoo@yahoo.com</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 at 09:45 AM.
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  2. #2

    Default

    Just to be sure, have you tried to connect to SMTP server using telnet ?
    (http://www.yuki-onna.co.uk/email/smtp.html)

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I don't know if yahoo will play nicely or not. I have used gmail successfully before, but these services are sometimes changed from time to time to prevent spam and such like. I would google the problem, there are lots of responses out there.

    e.g.
    http://forum.java.sun.com/thread.jspa?threadID=244595
    http://saloon.javaranch.com/45/000783.html

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello guys
    thanks for your reply both
    well by order

    pgras
    thanks again
    i saw your link so, if i try in linux this

    Code:
    well after a long delay i recieve again these prize 
    [user@localhost ~]$ telnet mail.yahoo.com 25
    Trying 209.191.92.114...
    telnet: connect to address 209.191.92.114: Connection timed out
    telnet: Unable to connect to remote host: Connection timed out
    [user@localhost ~]$
    or
    [user@localhost ~]$ telnet gmail.com 25 <---- check the way
    Trying 216.239.57.83...
    telnet: connect to address 216.239.57.83: Connection timed out
    Trying 64.233.161.83...
    
    [user@localhost ~]$ telnet mail.gmail.com 25 <---- no work!
    Trying 216.239.57.83...
    telnet: connect to address 216.239.57.83: Connection timed out
    Trying 64.233.161.83...
    telnet: connect to address 64.233.161.83: Connection timed out
    Trying 64.233.171.83...
    so the obvious way , no work , sad but true

    only works for hotmail
    Code:
    [user@localhost ~]$ telnet mail.hotmail.com 25
    Trying 65.54.244.8...
    Connected to mail.hotmail.com (65.54.244.8).
    Escape character is '^]'.
    220 bay0-mc6-f18.bay0.hotmail.com Sending unsolicited commercial or bulk e-mail to Microsoft's computer network is prohibited. Other restrictions are found at http://privacy.msn.com/Anti-spam/. Violations will result in use of equipment located in California and other states. Tue, 16 Jan 2007 17:31:59 -0800
    Connection closed by foreign host.
    like playing
    i found that in these ways works
    Code:
    [user@localhost ~]$ telnet smtp.gmail.com 25
    Trying 66.249.83.109...
    Connected to smtp.gmail.com (66.249.83.109).
    Escape character is '^]'.
    220 mx.google.com ESMTP i35sm12344483wxd
    
    [user@localhost ~]$  telnet smtp.mail.yahoo.com 25
    Trying 68.142.203.143...
    Connected to smtp.mail.yahoo.com (68.142.203.143).
    Escape character is '^]'.
    220 smtp106.plus.mail.mud.yahoo.com ESMTP
    quit
    221 smtp106.plus.mail.mud.yahoo.com
    Connection closed by foreign host.
    [user@localhost ~]$
    now with this configuration for gmail
    Code:
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
        <property name="host"> 
        	<value>smtp.gmail.com</value>
        </property>
        <property name="port"> 
        	<value>25</value>
        </property>
        <property name="username" >
        	<value>usernameforgamil</value>
        </property>
        <property name="password">
    	<value>billgateslovejava</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>to@hotmail.com</value>
    	</property>
    	<property name="from">
    		<value>usernameforgamil@gmail.com</value>
    	</property>
    	<property name="subject">
    		<value>Spring mail</value>
    	</property>						
    </bean>
    i recieve this

    Code:
    [java] EXCPETIONFailed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first i20sm12364936wxd
         [java] Stack
         [java] org.springframework.mail.MailSendException; nested exception details (1) are:
         [java] Failed message 1:
         [java] com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first i20sm12364936wxd
         [java] at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1275)
         [java] at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:895)
         [java] at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:524)
         [java] at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:382)
         [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)
         [java] CLASEclass org.springframework.mail.MailSendException
    for yahoo configuration, i recieve this
    Code:
         [java] EXCPETIONAuthentication failed; nested exception is javax.mail.AuthenticationFailedException
         [java] Stack
         [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)
         [java] CLASEclass org.springframework.mail.MailAuthenticationException
    Dear friend karldmoore
    how always thanks for your time and the links
    i saw the javaranch, so i have already open the SMTP (for incoming request, i can find the inverse - out, i use firestarter in FC5)

    but i still have the circus of error.

    pls help!!!

    BTW
    for hotmail i recieve 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)
    regards
    Last edited by dr_pompeii; Jan 16th, 2007 at 08:00 PM. Reason: i forgot exception for hotmail
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'd try googling this an going through the results. As I said before, they do mess with the settings on these things. I've had gmail working one week and not the next, very frustrating.

  6. #6
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello karldmoore

    As I said before, they do mess with the settings on these things.
    jesus, i must shoot them

    I've had gmail working one week and not the next, very frustrating.
    really very frustrating., each count should has an option to let send by java mail (to avoid possible false spams) - a solution

    i had an old project with java mail 2 years ago, but without Spirng, and always works, was with a count in http://www.cwazy.net/, (see its features ), i never had problems with that, but now is not free anymore

    maybe other member of the forum use other server for email , and share a link

    thanks for advanced
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    There are lots of threads out there, I guess you just have to look through them.

    e.g.
    http://forum.java.sun.com/thread.jsp...sageID=4292920

  8. #8
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  9. #9
    Join Date
    Jan 2007
    Location
    Boston, MA
    Posts
    1

    Default

    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="yourname@gmail.com" />
    		<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="yourname@gmail.com" />
    		<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("test@example.com");
    		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.
    Oliver Stewart
    Interface21 - Spring Training, Consulting, and Support, "From the Source"
    http://www.springframework.com/

  10. #10
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •