Five ‘s Weblog

October 28, 2007

Sending Email with Spring mail abstraction layer

Filed under: Spring — by powerdream5 @ 5:45 pm
Tags: , ,

10_28_1_2007.jpg

        Spring mail abstraction layer is based on javaMail. In fact, there are already a lot of examples in the internet to show how to send Email with spring mail abstraction layer, and they are easy to understand. Therefore, in this article, I will not do some repeated work, and just want to show some problems which you may come accross when your are programming.

        If you have your own mail server. It is very easy to write a email program, since you will not meet some “confusing problems”. If you not have one, this article is very useful for you. I still remembered how frustrated I was the first time when I coded a program for sending email with spring mail abstration layer through a public mail server(such as yahoo, gmail).

       Firstly, make sure you can connect to the mail server. Using telnet tool is very helpful for us to judge if we can connect it. Furthermore, we also need the username and password to log in. However, sometimes even you provide the usename and password, perhaps you still get the javax.mail.authenticationfailedexception. This exception means you cannot access this mail server from the traditional E-mail Clients, like foxmail and outlook, even you can telnet it (for example, smtp.163.com and smtp.126.com). The only way to handle this problem is changing your email server.

       Second, make sure your email server allows you to send email from the traditional E-mail Clients. Otherwise you will get the exception: 533 this mail is blocked by kbas system. KBAS system is anti-spam software. Because it is installed in your email server, and this software will recognize all the emails sent from the traditional E-mail Clients as spam so that it will prevent you from sending them. (For example, smtp.sohu.com)

       I have tried many email server, and found gmail is the one I really need. You will not meet the first two abstracles I listed above with gmail. In the following, I would like to show the sping configruration file.

       <bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>
               <property name=”host”>
                      <value>smtp.gmail.com</value>
               </property>
               <property name=”javaMailProperties”>
                      <props>
                              <prop key=”mail.smtp.auth”>true</prop>
                              <!–this is important, otherwise you will get the
                                     exception: 530 5.7.0 Must issue a STARTTLS command
–> 
                              <prop key=”mail.smtp.starttls.enable”>true</prop>
                              <prop key=”mail.smtp.timeout”>25000</prop>
                    </props>
                </property>
                <property name=”username”>
                       <value>caow190@gmail.com</value>
                </property>
                <property name=”password”>
                       <value>password</value>
                </property>
                <!–you don’t need to set the port number, 25 is default–>
         </bean>

 10_28_2_20071.jpg

Create a free website or blog at WordPress.com.