java - javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM -
i have java program(copied google) send emails using office365 smtp, working fine stand java program when deploy java program jar file in web-inf/lib of web application , calling method jsp throwing below error: javax.mail.sendfailedexception: sending failed; nested exception is: javax.mail.messagingexception: 530 5.7.57 smtp; client not authenticated send anonymous mail during mail from
can please share views on issue.
the java code :
import java.util.properties; 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; import org.apache.log4j.logger; public class sendemailusingsmtp { public static boolean sendemail(string toaddress, string fromaddress, string username, string userpassword,string smtphost, string portnumber, string emailsubject,string emailbody) { // recipient's email id needs mentioned. logger log = logger.getlogger(sendemailusingsmtp.class); log.info("toaddress : "+toaddress); log.info("fromaddress : "+fromaddress); log.info("username : "+username); log.info("userpassword : "+userpassword); log.info("smtphost : "+smtphost); log.info("portnumber : "+portnumber); log.info("emailsubject : "+emailsubject); log.info("emailbody : "+emailbody); string = toaddress; // sender's email id needs mentioned string = fromaddress;//change accordingly final string username = username;//change accordingly final string password = userpassword;//change accordingly // assuming sending email through relay.jangosmtp.net string host = smtphost; properties props = new properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketfactory.fallback", "false"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.socketfactory.port", portnumber); props.put("mail.smtp.host", host); props.put("mail.smtp.port", portnumber); // session object. smtpauthenticator authenticator = new smtpauthenticator(username, password); props.put("mail.smtp.submitter", authenticator.getpasswordauthentication().getusername()); session session = session.getinstance(props, authenticator); try { // create default mimemessage object. message message = new mimemessage(session); // set from: header field of header. message.setfrom(new internetaddress(from)); // set to: header field of header. message.setrecipients(message.recipienttype.to, internetaddress.parse(to)); // set subject: header field message.setsubject(emailsubject); // set actual message message.settext(emailbody); // send message transport.send(message); system.out.println("sent message successfully...."); } catch (messagingexception e) { throw new runtimeexception(e); } return true; } }
you can try following configuration, works me:
"mail.smtp.starttls.enable":"true"
also, used host:
host = "outlook.office365.com"
hope helps or else.
Comments
Post a Comment