如何通过Java从Outlook发送电子邮件? [英] How to send an email from Outlook via Java?

查看:2099
本文介绍了如何通过Java从Outlook发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了一个企业防火墙,不会让我通过传统方式(如Java Mail API或Apache Commons Email)发送电子邮件,甚至给组织内的其他人(这就是我想要的)。但是我的Outlook 2010显然可以发送这些电子邮件。我想知道是否有办法通过Java代码自动化Outlook 2010,以便Outlook可以发送电子邮件?我知道像mailto这样的东西可以弹出默认的Outlook发送对话框与预先填充的信息,但我正在寻找一种方式让发送操作发生在幕后。感谢任何信息。

解决方案

您可以通过outlook发送电子邮件 javamail 使用描述的配置 Outlook的官方网站



这是小的演示代码

  public static void main(String [] args){
final String username =你的电子邮件; // like yourname@outlook.com
final String password =*********; // password here

属性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.getInstance(props,
new javax.mail.Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication(){
返回新的PasswordAuthentication(用户名,密码);
}
});
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(你已经用outlook发送邮件了);

Transport.send(message);

System.out.println(Done);

} catch(MessagingException e){
throw new RuntimeException(e);
}
}



注意:我用 Javamail API 1.5.6


I am stuck behind a corporate firewall that won't let me send email via conventional means such as Java Mail API or Apache Commons Email, even to other people inside the organization(which is all I want anyways). But my Outlook 2010 obviously can send these emails. I was wondering if there is a way to automate Outlook 2010 via Java code so that Outlook can send the emails ? I know stuff like "mailto" can be used pop-up the default Outlook send dialog with pre-populated info, but I am looking for a way to have the send operation happen behind the scenes. Thanks for any info.

解决方案

You can send emails through outlook with javamail use the configurations described on Outlook's official site.

Here is small demo code

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);
    }
}

.
Note: I tested this with Javamail API 1.5.6

这篇关于如何通过Java从Outlook发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆