I am using following code but gettting error message as- run:
javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 530 5.7.0 : Recipient address rejected: Authentication Required at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887) at javax.mail.Transport.send0(Transport.java:191) at javax.mail.Transport.send(Transport.java:120) at MailClient.sendMail(MailClient.java:55) at MailClient.main(MailClient.java:94) Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 530 5.7.0 : Recipient address rejected: Authentication Required at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1505) ... 5 more
/**
*
* @author sachin
*/
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.Properties;
public class MailClient
{
public void sendMail(String mailServer, String from, String to,
String subject, String messageBody
) throws MessagingException, AddressException
{
// Setup mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mailServer);
// Get a mail session
Session session = Session.getDefaultInstance(props, null);
// Define a new mail message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
// addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
Transport.send(message);
}
protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException
{
for(int i = 0; i<= attachments.length -1; i++)
{
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
//use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//assume that the filename you want to send is the same as the
//actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);
//add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}
public static void main(String[] args)
{
try
{
MailClient client = new MailClient();
String server="smtp.bsgroup.in";
String from="[email protected]";
String to = "[email protected]";
String subject="Test Mail";
String message="Testing Mail";
// String[] filenames =
//{"c:\somefile.txt"};
client.sendMail(server,from,to,subject,message);
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
The error message you have received tells you that the SMTP server you have connected to requires authentication before you can use it to send an email, and that you haven't given it any authentication details.
See here (Internet Archive) for an example of how to send an email when the SMTP server requires authentication.
public class SendEmailServiceImpl extends Authenticator implements SendEmailService {
@Autowired
private TemplateEngine templateEngine;
private final static String id = "[email protected]";
private final static String pw = "!@#$5678";
private PasswordAuthentication pa;
public SendEmailServiceImpl() {
pa = new PasswordAuthentication(id, pw);
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
@Override
public void SendEmail(Person person, HttpServletRequest req, HttpServletResponse res) {
String link = "";
String contextPath = "http://" + req.getServerName() + ":" + req.getServerPort();
WebContext ctx = new WebContext(req, res, req.getServletContext(),
req.getLocale());
Properties p = System.getProperties();
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.host", "smtp.gmail.com");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.port", "587");
Authenticator auth = new SendEmailServiceImpl();
Session session = Session.getDefaultInstance(p, auth);
MimeMessage msg = new MimeMessage(session);
try {
msg.setSentDate(new Date());
InternetAddress from = new InternetAddress();
from = new InternetAddress(id);
msg.setFrom(from);
InternetAddress to = new InternetAddress(person.getEmail());
msg.setRecipient(Message.RecipientType.TO, to);
link = contextPath + req.getContextPath() + "/checkToken/" + person.getToken();
ctx.setVariable("tokenLink", link);
String template = templateEngine.process("email/email_join", ctx);
msg.setSubject("subject", "UTF-8");
msg.setText(template, "UTF-8");
msg.setHeader("content-Type", "text/html");
javax.mail.Transport.send(msg);
System.out.println("send email");
} catch (AddressException addr_e) {
addr_e.printStackTrace();
} catch (MessagingException msg_e) {
msg_e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With