用JavaMail 编了个发送邮件的小程序,
在windows下运行一切正常,邮件能发送成功,
但在linux下总发送不成功,错误提示:javax.mail.MessagingException: 500 Error: bad syntax

解决方案 »

  1.   

    一、源程序public class SendMail {
    private static Log log = LogFactory.getLog(SendMail.class);
    private static final String PROTOCOL = "mail.transport.protocol";
    private static final String HOST     = "mail.smtp.host";
    private static final String PORT     = "mail.smtp.port";
    private static final String AUTH     = "mail.smtp.auth";

    public static void main(String[] args){
    SendMail mail = new SendMail();
    mail.sendMail("[email protected]","hello!","Hello! this is a test.");
    }

    private String mailFile = "/mail.prop";
    private Properties mailProps;
    private boolean bAuth;

    public  SendMail(){
    initSendProp();
    }

    private void initSendProp(){
    try {
    mailProps = new Properties();
    mailProps.load(Utilities.getResourceAsStream(mailFile));
    bAuth = mailProps.containsKey(AUTH) && Boolean.parseBoolean(mailProps.getProperty(AUTH)); 
    log.info("init Mail prop!");
    } catch (IOException e) {
    log.error("init Mail error:"+e.getMessage());
    e.printStackTrace();
    }
    }

    private Properties getSessionProps(){
    Properties props = new Properties();
    props.put(PROTOCOL, mailProps.getProperty(PROTOCOL));
    props.put(HOST,     mailProps.getProperty(HOST));
    props.put(PORT,     mailProps.getProperty(PORT));
    if (bAuth){
        props.put(AUTH, "true");
    }
    return props;
    }

    private Session getMailSession(){
    if (!bAuth){
    return Session.getInstance(getSessionProps());
    }else{
    return Session.getInstance(getSessionProps(), 
                         new Authenticator(){
    public PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(mailProps.getProperty("username"),
                              mailProps.getProperty("password"));
    }
                             }
                               );
    }
    }

    public void sendMail(String to, String subject, String content){
        try {
         Message msg = new MimeMessage(getMailSession());
         msg.setFrom(new InternetAddress(mailProps.getProperty("from")));
         msg.setRecipient(Message.RecipientType.TO, 
                  new InternetAddress(to));
         msg.setSentDate(new Date());
         msg.setSubject(subject);
         msg.setText(content);
         Transport.send(msg);
         log.debug("Send mail to: "+to);
    } catch (MessagingException e) {
    e.printStackTrace();
    log.error(e);
    }
    }
    }
      

  2.   

    二、配置文件mail.transport.protocol=smtp
    mail.smtp.host=smtp.126.com
    mail.smtp.port=25
    mail.smtp.auth=true
    username=***
    password=111111
    from=my<***@126.com>
      

  3.   

    windows 下一切正常
    linux下出现如下异常:
    javax.mail.MessagingException: 500 Error: bad syntax        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
            at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
            at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
            at javax.mail.Service.connect(Service.java:297)
            at javax.mail.Service.connect(Service.java:156)
            at javax.mail.Service.connect(Service.java:105)
            at javax.mail.Transport.send0(Transport.java:168)
            at javax.mail.Transport.send(Transport.java:98)
            at org.wuyuan.mail.SendMail.sendMail(SendMail.java:87)
            at org.wuyuan.mail.SendMail.main(SendMail.java:29)
    08:02:28,485 ERROR SendMail:91 - javax.mail.MessagingException: 500 Error: bad syntax
      

  4.   

    问题以解决:
      在/etc/hosts文件中加入  127.0.0.1  myhostname  欢迎访问我的网站:
      http://www.sohuan.com  搜换网