java - 如何使用 java api 使用 Outlook 发送电子邮件

标签 java email outlook

我正在使用下面的代码使用 java 从 Outlook 发送电子邮件。但出现错误。

代码:

    public static void mail (){
        // TODO Auto-generated method stub
        //String host="POKCPEX07.corp.absc.local";
        String host="POKCPEX07.corp.absc.local";
        final String user="satpal.gupta@accenture.com";  
        String to="satpal.gupta@accenture.com";  

        //Get the session object  
        Properties props = new Properties();  
        props.put("mail.smtp.host",host);  
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.port", "587");


        Session session=Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("SGupta@amerisourcebergen.com","******");
            }
          });
        session.setDebug(true);

        try {
            MimeMessage message = new MimeMessage(session);
            message.saveChanges();
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
            message.setSubject("Test mail");  
            message.setText("This is test mail.");  

            //send the message
            Transport.send(message);

            System.out.println("message sent successfully...");
        }
        catch (MessagingException e) {e.printStackTrace();}

    }
}

错误:

javax.mail.MessagingException: Could not connect to SMTP host: POKCPEX07.corp.absc.local, port: 587;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
    at javax.mail.Service.connect(Service.java:258)
    at javax.mail.Service.connect(Service.java:137)
    at javax.mail.Service.connect(Service.java:86)
    at javax.mail.Transport.send0(Transport.java:150)
    at javax.mail.Transport.send(Transport.java:80)
    at TestEmail.mail(TestEmail.java:50)
    at TestEmail.main(TestEmail.java:16)

最佳答案

package com.sendmail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
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 SendAttachmentInEmail {


    private static final String SERVIDOR_SMTP = "smtp.office365.com";
    private static final int PORTA_SERVIDOR_SMTP = 587;
    private static final String CONTA_PADRAO = "xxxx@xxx.com"; //Cofig  Mail Id
    private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password

    private final String from = "xxxx@xxx.com"; 
    private final String to = "xxxx@xxx.com";

    private final String subject = "Teste";
    private final String messageContent = "Teste de Mensagem";

    public void sendEmail() {
        final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
            }

        });

        try {
            final Message message = new MimeMessage(session);
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setFrom(new InternetAddress(from));
            message.setSubject(subject);
            message.setText(messageContent);
            message.setSentDate(new Date());
            Transport.send(message);
        } catch (final MessagingException ex) {
           System.out.println(" "+ex);
        }
    }

    public Properties getEmailProperties() {
        final Properties config = new Properties();
        config.put("mail.smtp.auth", "true");
        config.put("mail.smtp.starttls.enable", "true");
        config.put("mail.smtp.host", SERVIDOR_SMTP);
        config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
        return config;
    }

    public static void main(final String[] args) {
        new SendAttachmentInEmail().sendEmail();
    }

}

关于java - 如何使用 java api 使用 Outlook 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737829/

相关文章:

vba - 如何转发文件夹中的电子邮件并将回复地址更改为原始发件人?

java - 从文件读取后添加数组中的数字总和

java - 开源项目静态分析

java - 如何使用 Apache HttpClient 发布非 JSON 请求?

java - 是否可以在 javax.mail.MimeMessage 中隐藏电子邮件地址?

html - HTTPS 图像未在 HTML 电子邮件中为某些用户显示

java - new URL(...).openConnection() 是否一定意味着 POST?

java - 异步发送邮件

ruby-on-rails - Rails delay_job以HTML格式发送电子邮件

vba - 电子邮件进入收件箱后触发 Outlook 脚本