others-how to solve smtp 501 exception sender address must contain a domain

1. The purpose of this post

I would demo how to solve this error when using java to send SMTP mail:

2019-08-07 21:25:38.151 17417-18863/? E/WoZhuan2: com.sun.mail.smtp.SMTPSendFailedException: 501 <xxxuser>: sender address must contain a domain;

nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 501 <xxxuser>: sender address must contain a domain

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(Unknown Source:121)
at com.sun.mail.smtp.SMTPTransport.mailFrom(Unknown Source:379)
at com.sun.mail.smtp.SMTPTransport.sendMessage(Unknown Source:180)
at javax.mail.Transport.send0(Unknown Source:120)
at javax.mail.Transport.send(Unknown Source:7)

Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 <xxxuser>: sender address must contain a domain

at com.sun.mail.smtp.SMTPTransport.mailFrom(Unknown Source:415)
at com.sun.mail.smtp.SMTPTransport.sendMessage(Unknown Source:180) 
at javax.mail.Transport.send0(Unknown Source:120) 
at javax.mail.Transport.send(Unknown Source:7) 

2. Environments

  • java 1.6+

3. Reason

Because we use the following code to send SMTP mail:

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
Transport.send(msg);

Here the value of _from is filled with value ‘xxxuser’, some of the SMTP service providers require this field to be a real email address, so we change the _from value from ‘xxxuser’ to ‘[email protected]’, the problem solved.

4. Solution and Code

Make sure you have a valid email address to be used as the ‘from’ address of the mail.