Unable to send SMTP mails using office365 settings
LaravelEmailOutlookSmtpOffice365Laravel Problem Overview
I am using SMTP mail for sending mail using Laravel. Everything working perfect other than office365 mail settings.
Settings I have used is as below:
SMTP HOST = smtp.office365.com
SMTP PORT = 587
SMTP ENCRYPTION = tls
SMTP USER = username(email)
SMTP PASS = password
Error i am getting is:
> 554 5.2.0 > STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; > Failed to process message due to a permanent exception with message > Cannot submit message
I have already searched google a lot for this error everybody says about clutter like this link Solution to this error But I personally don't find any clutter after followed all the steps mentioned.
I cannot log in this email as it's our client email id and I don't have permission to log in.
I also created one outlook email id and test this email setting. It worked like charm. I don't know what is wrong with Client email id.
Any suggestions would be great.
Laravel Solutions
Solution 1 - Laravel
Outlook doesn't provide to send using different from address other than your username to log in.
You need both email address same.
You can add one or more sender in your admin panel after that you can send easily from different addresses.
Solution 2 - Laravel
This error means the user whose credentials you specified in the SMTP connection cannot submit messages on behalf of the user specified in the From
and/or Sender
MIME headers or the FROM
SMTP command.
Solution 3 - Laravel
I face the similar issue and i resolved it right now, you are most likely facing this issue because your "user" email in the auth option and the "from" email at the mail option are different
make the user and from email same and it will work for you
const transporter = nodemailer.createTransport({
service: 'outlook',
port: 587,
auth: {
user: '[email protected]',
pass: '******'
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: "[email protected]", // sender address
to: '[email protected]', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
console.log(info);
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
If your email is not verified you will likely to get more errors
Solution 4 - Laravel
After trying for 4 days, mails started to triggered with port:25, so instead of trying with 587 or 465. Try with other port numbers.
host: "smtp.office***.***", port:25, secureConnection: false, requireTLS: true, tls: { ciphers: 'SSLv3' }, auth: { user: ***, pass: *** }
Solution 5 - Laravel
I used Hotmail and had this problem but solved it by editing MAIL_FROM_ADDRESS
to be the same as MAIL_USERNAME
Below is my env file set up.
MAIL_MAILER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME[email protected] (this must be the same as MAIL_FROM_ADDRESS!)
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS[email protected] (this must be the same as MAIL_USERNAME!)
Everything worked after doing the above.
Solution 6 - Laravel
What works for me is to set DEFAULT_FROM_EMAIL
as the EMAIL_HOST_USER
.
Working with Office 365 SMTP and Django 3.0.10.
Solution 7 - Laravel
you can also use this Mail-Driver: https://github.com/motze92/office365-mail
Here you can specify any From-Email Address where your tenant has the permission for. Sent E-Mails will also go into the recipients sent items folder.
Solution 8 - Laravel
for this issue check the jenkins system admin email it is be same as smtp user email
Solution 9 - Laravel
In Spring boot Java you can fix this issue by following code.
application.properties file
spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=smtp.office365.com
spring.mail.password=password
spring.mail.port=587
spring.mail.username[email protected]
spring.mail.properties.mail.smtp.starttls.enable=true
security.require-ssl=true
spring.mail.properties.mail.smpt.auth=true
Java class which impliments the mail functionality
@Component
public class MailSenderClass {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender javaMailSender;
public void sendMail(String to, String subject, String body) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);//true indicates multipart message
helper.setFrom(from) // <--- THIS IS IMPORTANT
helper.setSubject(subject);
helper.setTo(to);
helper.setText(body, true);//true indicates body is html
javaMailSender.send(message);
}
}
Note: you have to helper.setFrom(from)
is important , your issue will be resolved by adding that piece of code.