1

尝试从 Web 应用程序发送邮件时出现此异常:

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

2 回答 2

1

客户端登录时,某些邮件服务器可能会自动将域名附加到用户名,但某些服务器不会,因此无法通过身份验证。

于 2013-04-27T07:16:56.600 回答
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 = "jamsheer568@gmail.com";  //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[]{"example1@gmail.com"};  //To
   String[] bccRecipients = new String[]{"example2@gmail.com"};  //Bcc
   String subject = "Test Mail";  //Subject
   String messageBody = "Hi how  r  u?????";  //Body


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

  subject, messageBody));

   }  
   }  
于 2013-08-30T06:16:11.447 回答