使用Office365发送javamail

问题描述 投票:0回答:5

我在配置 SMTP 设置以通过 Office365 使用

javax.mail (1.4.4)
发送邮件时遇到问题,所以我想我应该在此处为其他人发布属性。

smtp jakarta-mail office365
5个回答
24
投票

使用Office365 smtp详细信息如下:

private static Properties props;  
private static Session session;   
static {      
  props = new Properties();
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.host", "m.outlook.com");
  props.put("mail.smtp.auth", "true");        
  session = Session.getInstance(props, new Authenticator() {          
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication("office365 email address",
                  "office365 password");          
      }       
  });

}

12
投票

使用 spring-boot,您只需将其添加到您的

application.properties
:

spring.mail.host = smtp.office365.com
spring.mail.username = [email protected]
spring.mail.password = s3cr3t
spring.mail.port = 587
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true

4
投票

工作代码示例:

Email email = new SimpleEmail();

email.setHostName("smtp.office365.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "****"));
email.setStartTLSEnabled(true);
try {
    email.setFrom("[email protected]");
    email.setSubject("Job Failure");
    email.setDebug(true);
    email.setMsg("This is a test mail ... :-)" );
    email.addTo("[email protected]");
    email.send();
} catch (EmailException e) {
    e.printStackTrace();
}

0
投票

我在您的代码中注意到的唯一错误是不正确的主机

javaMailProperties.setProperty("mail.smtp.from", "[email protected]");
    javaMailProperties.setProperty("mail.smtp.user",  "[email protected]");
    javaMailProperties.setProperty("mail.smtp.password","Password");
    javaMailProperties.setProperty("mail.smtp.host", "smtp.office365.com");
    javaMailProperties.setProperty("mail.smtp.port", "587");
    javaMailProperties.setProperty("mail.smtp.auth", "true");
    javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");

换个主机就好了。


0
投票

javax.mail (1.4.4)(1.4.7) 与 oulook smtp 存在一些问题。

以下代码适用于我使用 javax.mail(1.6.2)

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by tanvir on 6/11/24.
 */

public class OutlookMailSender {



    public static void main(String[] args) {

        String to = "[email protected]";
        String from = "your domain email address";
        final String username = "your domain email username"; // your Outlook email
        final String password = "your domain email passowrd"; // your Outlook email password

        // Assuming you are sending email through smtp-mail.outlook.com
        String host = "smtp-mail.outlook.com";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        props.put("mail.debug", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.ssl.enable", "true");

        // Get the Session object.
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Message message = new MimeMessage(session);


            message.setFrom(new InternetAddress(from));


            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));


            message.setSubject("Test Mail");


            message.setText("This is a test mail");


            Transport.send(message);

            System.out.println("Sent message successfully...");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.