0

这是我向用户发送邮件的代码。

mailMessage=new SimpleMailMessage();
        Strinf passwoed="Abij@1"
        String emailHeader = "Dear,";
        String emailFooter= "Thanks & Regards ";
        mailMessage.setFrom("admin@hcl.com");
        mailMessage.setTo("user@hcl.com");
        mailMessage.setSubject("Email For Password");   
        mailMessage.setText(emailHeader + " \n EMAIL: " + this.newUserModel.getEmailId()+ "\n PASSWORD : " + passWord+ "\n " + emailFooter );
        try {
            mailSender.send(mailMessage);
        }
        catch (Exception e) {
            logger.info("exception is :"+e.getMessage());
        }

当我运行此代码时,我得到一个异常是:null message..

4

2 回答 2

2

根据您对 SO 的要求:-当我运行此代码时,出现异常是:null 消息

只需配置一个bean 文件,即一个xml 文件。

<bean id="mailID" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="put host server name that u want to use" />
    <property name="port" value="port_No" />
    <property name="username" value="u_name" />
    <property name="password" value="u_pwd" />

    <property name="properties_name">
       <props>
              //define Ur key Here
           </props>
    </property>
</bean>

<bean id="myMailID" class="Ur Main Class Name following the packageName">
    <property name="prop_Name" ref="mailSender" />
</bean>

这是一个很好的Tut

于 2013-06-07T09:43:03.973 回答
0

解决我的问题,谢谢大家...
我根据sams在我的applicationcontext.xml中添加了以下代码..我的代码运行得很好。
以下是我的代码...

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>203.90.70.245(here is the host name)</value>
        </property>
        <property name="protocol">
            <value>smtp</value>
        </property>
        <property name="port">
            <value>2525</value>
        </property>
        <property name="username">
            <value>vijayk@gmail.com(eamil id from you want to send)</value>
        </property>
        <property name="password">
            <value>1234(password of your email id)</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.transport.protocol">smtp</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.host">203.90.70.245</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.from">vijayk@gmail.com</prop>
            </props>
        </property>
    </bean>

并从代码中移除, mailMessage.setFrom("admin@hcl.com");
并将这段代码写在serviceLayer中。

于 2013-06-07T10:00:57.753 回答