java outlook发送邮件 [英] java outlook send mail

查看:259
本文介绍了java outlook发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Outlook发送邮件,但收到如下错误。

I'm trying to send mail through outlook but getting error as below.

package test.first.javamail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendAttachmentInEmail {
    public static void main(String[] args) {
        // Recipient's email ID needs to be mentioned.
        // String to = "sagapawar1234@gmail.com";

        // Sender's email ID needs to be mentioned
        String from = "abcd@outlook.com";
        final String password = "*****";// change accordingly

        // Check how many arguments were passed in
        if (args.length == 0) {
            System.out.println("Please run Sendmail jar as: java -jar SendMail.jar <recivers@gmail.com> <path of attachment file>");
            System.exit(0);
        }

        String to = args[0];
        String attachment_path = args[1];
        final String username = from;
        ;// change accordingly

        System.out.println("Trying to send mail to : " + to + "\n");
        // final String username = "abcd";// change accordingly
        // final String password = "******";// change accordingly
        System.out.println("Please wait for a moment, we are checking creadentials...! \n");
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
//      props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com
        props.put("mail.smtp.host", "smtp.office365.com");
        props.put("mail.smtp.port", "587");
//      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//      props.put("mail.smtp.socketFactory.fallback", "true");

        // Get the Session object.
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        System.out.println("Seat tight your identity has been proved and mail is just on the way...! ");
        try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));

            // Set Subject: header field
            message.setSubject("Testing Subject");

            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Now set the actual message
            messageBodyPart.setText("This is message body");

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            String filename = attachment_path;// "C:/Users/swapnil.kotwal/Desktop/buildsuccess.txt";Azure.png
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            message.setContent(multipart);

            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");

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

错误是

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
      nested exception is:
            java.net.ConnectException: Connection timed out: connect
            at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:104)
    Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
      nested exception is:
            java.net.ConnectException: Connection timed out: connect
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
            at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
            at javax.mail.Service.connect(Service.java:367)
            at javax.mail.Service.connect(Service.java:226)
            at javax.mail.Service.connect(Service.java:175)
            at javax.mail.Transport.send0(Transport.java:253)
            at javax.mail.Transport.send(Transport.java:124)
            at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:99)
    Caused by: java.net.ConnectException: Connection timed out: connect
            at java.net.DualStackPlainSocketImpl.connect0(Native Method)
            at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
            at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
            at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
            at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
            at java.net.PlainSocketImpl.connect(Unknown Source)
            at java.net.SocksSocketImpl.connect(Unknown Source)
            at java.net.Socket.connect(Unknown Source)
            at java.net.Socket.connect(Unknown Source)
            at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
            at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)

我相信代码是正确的但不确定究竟是什么阻止邮件传递?任何帮助将不胜感激。

I believe the code is proper but not sure what exactly blocking the mail delivery ? Any help will be appreciated.

推荐答案

基于

 java.net.ConnectException: Connection timed out: connect
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
            at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)

我认为你需要检查一些事情


  • SMTP服务器应该合适

  • 某些组织已阻止 587 端口号,请与您的IT团队联系,确认该端口是否已打开以与给定的SMTP
    服务器进行通信

  • SMTP server should be appropriate
  • Some organisations has blocked 587 port number, please check with your IT team if that port is open to communicate with given SMTP server

如果你得到合适的开放端口,那么问题就是要解决。

If you get the appropriate open port,the issue suppose to get resolved.

基本上可以处理这个属性

Basically work on this properties

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
//      props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com
        props.put("mail.smtp.host", "smtp.office365.com");
        props.put("mail.smtp.port", "587"); //change this port

如果您的组织有任何代理相关设置,您可以参考此答案以及 https://stackoverflow.com/a/12097764/1665592

If there is any proxy related settings your org have, you can refer this answers as well https://stackoverflow.com/a/12097764/1665592

这篇关于java outlook发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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