gpt4 book ai didi

java - 在 MAIL FROM [MA1PR01CA0086.INDPRD01.PROD.OUTLOOK.COM] 期间,客户端未经过身份验证以发送匿名邮件

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:46 26 4
gpt4 key购买 nike

我正在尝试通过java邮件api使用office365发送邮件。我的代码是 -

public void sendMail() {

String to = abc@companyName.com;
String subject = "subject";
String msg ="email text....";
final String from = xyz@companyName.com;
final String password = "password";


Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.office365.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
InternetAddress addressFrom = null;
try {
addressFrom = new InternetAddress(from);
} catch (AddressException e) {
e.printStackTrace();
}
try {
MimeMessage message1 = new MimeMessage(session);
message1.setSender(addressFrom);
message1.setSubject(subject);
message1.setContent(msg, "text/plain");
message1.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

transport.connect();
Transport.send(message1);
transport.close();
} catch(Exception e) {
e.printStackTrace();
}
}

我在 pom 中添加了邮件和激活的依赖项:

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>

<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>

但是我遇到了这个异常。

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; 
Client was not authenticated to send anonymous mail during MAIL FROM [MA1PR01CA0086.INDPRD01.PROD.OUTLOOK.COM]

最佳答案

在执行java程序时添加-Dmail.smtp.starttls.enable=true的vm参数。例如:

java -Dmail.smtp.starttls.enable=true -cp send_mail.jar package_name.class_name

关于java - 在 MAIL FROM [MA1PR01CA0086.INDPRD01.PROD.OUTLOOK.COM] 期间,客户端未经过身份验证以发送匿名邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com