2013-04-27 43 views
1

我试图从Web应用程序发送邮件时,此异常:Java邮件API例外

com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Missing 
or literal domains not allowed 

我使用类似下面的代码性能。

Properties props = new Properties(); 
props.setProperty("mail.transport.protocol", "smtp"); 
props.setProperty("mail.host", "smtp.verizon.net"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "465"); 

我还使用身份验证方法使用用户名和密码对用户进行身份验证。

只有当我通过身份验证时,我才得到成功消息。当我去到名为transport.sen(message)的行时,我得到了异常。

this is my full code.. 



try { 

      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(
        to)); 
//   message.addRecipient(Message.RecipientType.CC, new InternetAddress(
//     cc)); 
//   message.addRecipient(Message.RecipientType.BCC, 
//     new InternetAddress(bcc)); 
      message.setSubject("TEST...!!!!!!!"); 

      Multipart multipart = new MimeMultipart(); 

      BodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart 
      .setText("Dear Sir, Mail Testing"); 
      multipart.addBodyPart(messageBodyPart); 


      messageBodyPart.setText("Hao test"); 
      message.setText("Kader here"); 
      message.setContent(multipart); 


      MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
       mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
       mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
       mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
       mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
       mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
       CommandMap.setDefaultCommandMap(mc); 
      Transport transport = session.getTransport(); 

      transport.connect(); 
      Transport.send(message); 
      transport.close(); 


      System.out.println("Sent message successfully...."); 
     } catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } 
+0

如何设置邮件recipeints,发件人和内容?请张贴代码 – Abubakkar 2013-04-27 07:10:33

+0

这似乎与Verizon有关。也许这些帮助:http://forums.verizon.com/t5/Verizon-net-Email/Win7-receive-but-no-send/td-p/312309和http://www.vistax64.com/vista- mail/197859-window-mail-gone-haywire-help.html – dcernahoschi 2013-04-27 07:13:14

回答

1

有一些邮件服务器有可能在客户端日志记录时自动将域名追加到用户名上,但是某些服务器不会登录,从而导致认证失败。

+0

我只把用户名这样只有..xyz不喜欢这个[email protected] – kader 2013-04-27 07:19:26

0
Try this code :- 

MailUtil.java 
------------ 
package com.test; 

import java.io.UnsupportedEncodingException; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

/** 
    * Please test with your GMAIL 
    * @author Jamsheer T 
    * +91-9846716175 
    * 
    */ 

public class MailUtil { 

private String SMTP_HOST = "smtp.gmail.com"; 
private String FROM_ADDRESS = "[email protected]"; //Give Your gmail here 
private String PASSWORD = "***********"; //Give Your password here 
private String FROM_NAME = "Jamsheer T"; 

    public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, 

    String message) { 
    try { 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", SMTP_HOST); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "false"); 
     props.put("mail.smtp.ssl.enable", "true"); 
     props.put("mail.smtp.starttls.enable","true"); 

     Session session = Session.getInstance(props, new SocialAuth()); 
     Message msg = new MimeMessage(session); 

     InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME); 
     msg.setFrom(from); 

     InternetAddress[] toAddresses = new InternetAddress[recipients.length]; 
     for (int i = 0; i < recipients.length; i++) { 
      toAddresses[i] = new InternetAddress(recipients[i]); 
     } 
     msg.setRecipients(Message.RecipientType.TO, toAddresses); 


     InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length]; 
     for (int j = 0; j < bccRecipients.length; j++) { 
      bccAddresses[j] = new InternetAddress(bccRecipients[j]); 
     } 
     msg.setRecipients(Message.RecipientType.BCC, bccAddresses); 

     msg.setSubject(subject); 
     msg.setContent(message, "text/plain"); 
     Transport.send(msg); 
     return true; 
    } catch (UnsupportedEncodingException ex) { 
     ex.printStackTrace(); 

     Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
     return false; 

    } catch (MessagingException ex) { 
     ex.printStackTrace(); 

     Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
     return false; 
    } 
} 

class SocialAuth extends Authenticator { 


    protected PasswordAuthentication getPasswordAuthentication() { 

     return new PasswordAuthentication(FROM_ADDRESS, PASSWORD); 

    } 
    } 
    } 

Main.java 
---------------- 
package com.test; 


public class Main { 

    public static void main(String[] args) { 
    String[] recipients = new String[]{"[email protected]"}; //To 
    String[] bccRecipients = new String[]{"[email protected]"}; //Bcc 
    String subject = "Test Mail"; //Subject 
    String messageBody = "Hi how r u?????"; //Body 


     System.out.print("Result"+new MailUtil().sendMail(recipients, bccRecipients, 

    subject, messageBody)); 

    } 
    }