2014-11-23 77 views
0

嗨,我最近收到了一封域名郵件。雖然我能夠通過他們的門戶發送和接收郵件,但是我通過Java Mail遇到了問題。 我使用了以下配置:使用域名郵件時拒絕使用Java郵件連接

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作爲主持人,我試過相同。他還表示不要提供任何安全設置,或將其配置爲空。當我嘗試通過Gmail發送郵件時,類似的設置工作。我可以通過展望訪問 任何建議?

+0

刪除'mail.smtp.starttls.enable = TRUE',然後再試一次。也許他們的SMTP服務器很糟糕,並且不支持SSL。 – mostruash 2014-11-23 12:33:57

+0

是的,我嘗試過。藥物工作。即使嘗試刪除 javaMailProperties.setProperty(「mail.smtp.socketFactory.class」,「javax.net.ssl.SSLSocketFactory」); javaMailProperties.setProperty(「mail.smtp.socketFactory.fallback」,「false」); 但它允許繼電器不允許 – 2014-11-23 12:50:43

+0

對於大多數郵件服務器,端口25用於純文本連接。端口465用於SSL。 – 2014-11-23 13:14:42

回答

0

並非所有的失敗都有同樣的原因。如果你改變了一些東西,並以不同的方式失敗了,那並不意味着你的改變是錯誤的,它只是意味着你遇到了一個問題並且遇到了另一個問題。您需要remove all the socket factory stuff。剩下的你仍然需要。如果失敗,發佈debug output顯示新的失敗。

+0

我刪除所有套接字的東西后,我得到以下錯誤。 javax.mail.SendFailedException:地址無效;嵌套異常是:com.sun.mail.smtp.SMTPAddressFailedException:550不允許中繼 – 2014-11-25 09:59:22

+0

請參閱此[JavaMail FAQ條目](http://www.oracle.com/technetwork/java/javamail/faq/index.html#norelay )。你的信息是否有發件人地址? [調試輸出](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug)顯示什麼? – 2014-11-25 20:01:00

+0

線程「main」中的異常org.springframework.mail.MailSendException:失敗的消息:javax.mail.SendFailedException:無效的地址; 嵌套的異常是: \t com.sun.mail.smtp.SMTPAddressFailedException:550中繼不允許 ;消息異常詳細信息(1)爲: 失敗消息1: javax.mail.SendFailedException:地址無效; 嵌套異常是: \t com.sun.mail.smtp.SMTPAddressFailedException:在com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1835) \t 550中繼不允許 \t在的com.sun。 mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1098) – 2014-11-27 15:02:04

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(...){...}; 
} 
相關問題