Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Architecture & Framework » Spring Framework  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于spring中使用JavaMailSenderImpl的错误
returner





发贴: 43
积分: 0
于 2005-06-16 20:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在写个spring mail程序,调用JavaMailSenderImpl.但是出现错误了,不知道如何解决,希望大虾们能帮帮小弟啊,谢了!!
这是我的代码和配置文件:

public class UserServiceImpl implements IUserService {
private MailSender mailSender;
private SimpleMailMessage mailMessage;

/* (non-Javadoc)
* @see com.webex.user.service.IUserService#sendMailToUser()
*/
public boolean sendMailToUser() {
SimpleMailMessage simpleMail=new SimpleMailMessage(this.mailMessage);
simpleMail.setText("test java mail by peihe chen. do you recieve" +
    " this mail,pls response as soon as possible");
this.mailSender.send(simpleMail);
return true;
}

/* (non-Javadoc)
* @see com.webex.user.service.IUserService#setMailSender(org.springframework.mail.MailSender)
*/
public void setMailSender(MailSender mailSender) {
this.mailSender=mailSender;

}

/* (non-Javadoc)
* @see com.webex.user.service.IUserService#setSimpleMailMessage(org.springframework.mail.SimpleMailMessage)
*/
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage=mailMessage;

}

配置文件:
<bean id="userService"
    class="com.webex.user.service.spring.UserServiceImpl">
    <property name="mailSender">
      <ref local="mailSender"/>
    </property>
    <property name="mailMessage">
      <ref local="enrollmentMailMessage"/>
    </property>
  </bean>
  
  
  <!--mail info-->
  <bean id="mailSender"
    class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host">
      <value>smtp.163.com</value>
    </property>
    <property name="password">
      <value>232323</value>
    </property>
    <property name="username">
      <value>chenpeihe</value>
    </property>
  </bean>
  <bean id="enrollmentMailMessage"
    class="org.springframework.mail.SimpleMailMessage">
    <property name="to">
      <value>XXXX@21cn.com</value>
    </property>
    <property name="from">
      <value>XXXX@163.com</value>
    </property>
    <property name="subject">
      <value>spring training</value>
    </property>
  </bean>

运行后产生下面的错误:

org.springframework.mail.MailSendException: Could not send mails: 553 You are not authorized to send mail as <MAIL FROM:<chenpeihe@163.com>>, authentication is required

com.sun.mail.smtp.SMTPSendFailedException: 553 You are not authorized to send mail as <MAIL FROM:<chenpeihe@163.com>>, authentication is required

  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333)
  at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:535)
  at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:382)
  at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
  at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)
  at com.webex.user.service.spring.UserServiceImpl.sendMailToUser(UserServiceImpl.java:64)
  at com.webex.junit.user.service.TestUserServiceImpl.testSendMailToUser(TestUserServiceImpl.java:42)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:324)
  at junit.framework.TestCase.runTest(TestCase.java:154)
  at junit.framework.TestCase.runBare(TestCase.java:127)
  at junit.framework.TestResult$1.protect(TestResult.java:106)
  at junit.framework.TestResult.runProtected(TestResult.java:124)
  at junit.framework.TestResult.run(TestResult.java:109)
  at junit.framework.TestCase.run(TestCase.java:118)
  at junit.framework.TestSuite.runTest(TestSuite.java:208)
  at junit.framework.TestSuite.run(TestSuite.java:203)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)


returner edited on 2005-06-16 20:25

作者 Re:关于spring中使用JavaMailSenderImpl的错误 [Re:returner]
returner





发贴: 43
积分: 0
于 2005-06-16 20:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这里错误是需要authentication,但我没有看到spring中需要authentication的,如果真需要authentication,请大家教教我如何authentication!!!


作者 Re:关于spring中使用JavaMailSenderImpl的错误 [Re:returner]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2005-06-16 22:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
your username and password are for authentication.

However, on some unix systems, you should not set these two fields, but pass in a valid email id in the From field.

So remove your username and password property settings in the JavaMailSenderImpl in the Spring xml file.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:关于spring中使用JavaMailSenderImpl的错误 [Re:returner]
returner





发贴: 43
积分: 0
于 2005-06-17 09:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我去掉username 和password了,但是错误还是一样的.

在用javamail时,是需要authentication的:

session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(SMTP_USERNAME,
SMTP_PASSWORD);
}
}
MimeMessage message = new MimeMessage(session);
Transport.send(message);

但是在spring中的JavaMailSenderImpl,这个类里有session的属性,但是不知道在配置文件中如何给这个属性赋值???



作者 Re:关于spring中使用JavaMailSenderImpl的错误 [Re:returner]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2005-06-17 21:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Then I think you mail host can't verify your user name ...@163.com, I assume your mail host is not something like ...@163.com.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:关于spring中使用JavaMailSenderImpl的错误 [Re:returner]
returner





发贴: 43
积分: 0
于 2005-06-21 13:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
thanks jedi!

i had resolved this question.

it indeed need a author for host.

need create this class MailProperties:
public class MailProperties extends Properties {
private String auth;

public MailProperties(String auth){
super.setProperty("mail.smtp.auth",auth);
}
}

then add a bean into configure file:

<bean id="mailProperties"
  class="com.webex.user.service.spring.MailProperties">
<constructor-arg index="0">
  <value>true</value>
</constructor-arg>
</bean>

then modify bean mailSender :
<bean id="mailSender"
  class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host">
      <value>smtp.163.com</value>
    </property>
    <property name="username">
      <value>username</value>
    </property>
    <property name="password">
      <value>password</value>
    </property>
    
    <property name="javaMailProperties">
      <ref local="mailProperties"/>
    </property>
    
  </bean>




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923