这是我向用户发送邮件的代码。
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 ..
答案 0 :(得分:2)
按照你的要求: - 当我运行此代码时,我得到一个例外是: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
答案 1 :(得分: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中编写此代码。