简体   繁体   English

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

[英]Java Mail Connection Refused when Using a domain mail

Hi I recently got a domain mail. 嗨,我最近收到了域邮件。 While I am able to send and receive mail through their portal, I am having problems doing so through Java Mail. 虽然我可以通过他们的门户发送和接收邮件,但在通过Java Mail进行发送时遇到了问题。 I am using the following configuration : 我正在使用以下配置:

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);

   }

I am still getting an exception like : 我仍然遇到类似的异常:

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)

I called the customer care and they said use smtp8.net4india.com as host, i tried the same. 我打电话给客户服务部门,他们说使用smtp8.net4india.com作为主机,我尝试了同样的方法。 He also said not to prvide any security settings, or configure it to null. 他还说,不要植入任何安全设置,也不要将其配置为null。 Similar setting work when i tried sending mail through gmail. 当我尝试通过gmail发送邮件时,类似的设置工作。 I can access through outlook though Any suggestion ? 我可以通过Outlook访问任何建议吗?

Not all of your failures have the same cause. 并非所有的故障都有相同的原因。 If you changed something and it failed in a different way, that doesn't mean your change was wrong, it just means you got past one problem and ran into another. 如果您更改了某项并且以不同的方式失败了,这并不意味着您所做的更改是错误的,仅意味着您克服了一个问题而遇到了另一个问题。

You need to remove all the socket factory stuff . 您需要删除所有套接字工厂的东西 The rest you still need. 剩下的你仍然需要。 If that fails, post the debug output showing the new failure. 如果失败,则发布调试输出,显示新的失败。

Try this: 尝试这个:

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(...){...};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM