我被困在企业防火墙后面,不允许我通过 Java Mail API 或 Apache Commons Email 等传统方式发送电子邮件,甚至无法发送给组织内的其他人(无论如何我都想要)。但是我的 Outlook 2010 显然可以发送这些邮件。我想知道是否有一种方法可以通过 Java 代码使 Outlook 2010 自动化,以便 Outlook 可以发送电子邮件?我知道像“mailto”这样的东西可以用来弹出带有预填充信息的默认 Outlook 发送对话框,但我正在寻找一种让发送操作在幕后发生的方法。感谢您提供任何信息。
最佳答案
您可以使用 javamail
通过 outlook 发送电子邮件,使用 Outlook's official site. 中描述的配置
这是一个小的演示代码
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public static void main(String[] args) {
final String username = "your email"; // like yourname@outlook.com
final String password = "*********"; // password here
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("receiver mail")); // like inzi769@gmail.com
message.setSubject("Test");
message.setText("HI you have done sending mail with outlook");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
.
注意:我使用 Javamail API 1.5.6
关于java - 如何通过 Java 从 Outlook 发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39218352/