1

I got this exception when trying to send mail from web application:

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

I am using properties like below code.

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

I also authenticate the users using username and password using authenticate method.

I got success message only when i'm authenticate. I got exception when i go to line called 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();
        }
2

2 Answers 2

1

There is possibility that Some mail servers automatically append the domain name to the user name when client logging but some server will not, hence fail the authentication.

Sign up to request clarification or add additional context in comments.

1 Comment

i put username like this only..xyz not like this [email protected]
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));

   }  
   }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.