Java邮件API:通过公司outlook帐户发送电子邮件
我希望我的程序能够从我的企业outlook帐户发送电子邮件。我看了很多JMA的例子,它们似乎不是我想要的Java邮件API:通过公司outlook帐户发送电子邮件,java,jakarta-mail,Java,Jakarta Mail,我希望我的程序能够从我的企业outlook帐户发送电子邮件。我看了很多JMA的例子,它们似乎不是我想要的 在哪里可以找到通过outlook发送邮件的简单示例 我是否应该将邮件系统移动到单独的服务应用程序?如果是,为什么 您所需要的只是公司帐户的SMTP设置。使用Java邮件API在程序中设置这些,就这样。e、 g Properties props = System.getProperties(); props.put("mail.smtp.host", "your server here");
您所需要的只是公司帐户的SMTP设置。使用Java邮件API在程序中设置这些,就这样。e、 g
Properties props = System.getProperties();
props.put("mail.smtp.host", "your server here");
Session session = Session.getDefaultInstance(props, null);
示例:您需要先下载
javax.mail
JAR。然后尝试以下代码:
import java.io.IOException;
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;
public class SendMail {
public static void main(String[]args) throws IOException {
final String username = "enter your username";
final String password = "enter your password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
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("enter your outlook mail address"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("Enter the recipient mail address"));
message.setSubject("Test");
message.setText("HI");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
据我所见,您缺少以下导入:
import java.io.IOException;
只需在此类开始时将其包含在类导入中,您的问题就会得到解决。我尝试将
outlook.office365.com
作为主机名,但遇到了身份验证未接受的异常。尝试使用smtp mail.outlook.com
我可以使用Javamail API
通过outlook发送邮件
有关更多详细信息,请访问outlook官方网站
获取完整的工作演示代码。您是否尝试过任何命令行来尝试通过Java向Outlook应用程序发送命令?请参阅相关文章:Outlook的服务器应该是什么?请提供示例,检查outlook配置,或询问管理员\网络团队。或者,如果您使用的是Outlook web access,请检查Outlook关于页面的信息,例如:谢谢,它可以正常工作。但我需要代表特殊用户/帐户发送电子邮件。我应该如何设置用户名/密码?请参阅张贴的示例。您可以使用例如msg.setFrom(新InternetAddress(“admin@example.com“,”Example.com Admin“);我就是搞不懂InternetAddress构造函数的第二个参数是什么。据我所知,在不提供用户名/密码的情况下,我将代表已登录的用户发送电子邮件。我说得对吗?但我需要代表testUser/12345发送电子邮件。你能给我举个简单的例子吗?你能详细说明这个答案吗?版主通常会寻找更具实质性的答案,给出一个更完整的必要解决方案的例子。不适合我。我一直收到一个错误,说“5505.2.1邮箱无法访问”。我的一位同事正在使用相同的凭据(虽然是C#),并调用exchange web服务,该服务对他有效。感谢您提供的代码,但在传输中遇到了困难。发送(消息);行。可能是因为代理。请你核对一下好吗?