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

Advertisements

6 Comments »

  1. dsdfsdfsdf

    Comment by a — January 7, 2008 @ 9:56 am |Reply

  2. exactly what i needed, thanks

    Comment by Eric — February 13, 2008 @ 6:19 am |Reply

  3. This didn’t work with me!
    I had to set the port to 465 and use smtps !

    Comment by yusuf — March 3, 2008 @ 1:22 am |Reply

  4. How to Configure Proxy Setting ( Proxy Host, Proxy Post, Proxy User Name and Proxy Password ) while sending mail

    Comment by Sachidhanandhan — May 6, 2008 @ 3:12 am |Reply

  5. fhhhfhfhgfhg

    Comment by fdfdfdfd — September 14, 2010 @ 2:05 pm |Reply

  6. It’s actually a nice and helpful piece of info. I am satisfied that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.

    Comment by factory relocation — July 19, 2013 @ 1:01 am |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: