2014-11-23 78 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(...){...}; 
} 
相关问题