使用域邮件时拒绝Java Mail连接

时间:2014-11-23 09:13:12

标签: java spring javamail

您好我最近收到了域名邮件。虽然我能够通过他们的门户发送和接收邮件,但我在使用Java Mail时遇到了问题。 我使用以下配置:

static {
        mailSender = new JavaMailSenderImpl();
        //mailSender.setHost("smtp.net4india.com");
        mailSender.setHost("smtp8.net4india.com");
        mailSender.setUsername("xxxx");
        mailSender.setPassword("xxxx");
        mailSender.setPort(25);
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("mail.smtp.auth", "true");
        javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
        javaMailProperties.setProperty("mail.transport.protocol", "smtp");
        javaMailProperties.setProperty("mail.smtp.socketFactory.port", "25");
        javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailProperties.setProperty("mail.smtp.socketFactory.fallback", "false");
        mailSender.setJavaMailProperties(javaMailProperties);
    }

    public static void sendMessage(String subject, String testMessage){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("xxxx");
        message.setSubject(subject);
        message.setText(testMessage);
        mailSender.send(message);

   }

我仍然得到一个例外:

javax.mail.MessagingException: Could not connect to SMTP host: smtp8.net4india.com, port: 25;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)

我致电客户服务,他们说使用smtp8.net4india.com作为主机,我尝试了同样的方法。 他还说不要提供任何安全设置,或将其配置为null。当我尝试通过Gmail发送邮件时,类似的设置工作。我可以通过outlook访问 有什么建议吗?

2 个答案:

答案 0 :(得分:0)

并非所有失败都有相同的原因。如果你改变了某些东西并且它以不同的方式失败了,那并不意味着你的改变是错误的,它只是意味着你遇到了一个问题并遇到了另一个问题。

您需要remove all the socket factory stuff。剩下的你还需要。如果失败,请发布显示新失败的debug output

答案 1 :(得分:0)

试试这个:

public static void sendMail(MailInfo mailInfo) throws Exception{

    MyAuthenticator auth=null;
    Properties prop=mailInfo.getProperties();

    if(mailInfo.isValidate()){
        auth=new MyAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());
    }

    Session mailSess=Session.getDefaultInstance(prop, auth);

    try {
        Message msg=new MimeMessage(mailSess);
        msg.setFrom(new InternetAddress(mailInfo.getFromAddress(),mailInfo.getSender()));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        msg.setSubject(mailInfo.getSubject());
        msg.setSentDate(new Date());
        msg.setText(mailInfo.getContent());

        Transport.send(msg);
    } catch (Exception e) {
        throw new Exception(e);
    }
}

public class MailInfo {

private String mailServerHost;
private String mailServerPort;
private String fromAddress;
private String sender;
private String toAddress;
private String username;
private String password;
private boolean isValidate=false;
private String subject;
private String content;

public Properties getProperties(){
    Properties prop=new Properties();
    prop.setProperty("mail.smtp.host", this.mailServerHost);
    prop.setProperty("mail.smtp.port", this.mailServerPort);
    prop.setProperty("mail.smtp.auth", this.isValidate?"true":"false");
    return prop;
}
 .....getXXX(){...};
 .....setXXX(...){...};
}