使用JavaMail向我工作的电子邮件服务器发送电子邮件

时间:2016-07-28 15:46:37

标签: java email server

我在这里搜索了许多其他帖子,其中有类似问题的用户,但我似乎无法让它工作。

<nav>
  <img src="http://www.fillmurray.com/40/40" alt="logo">
  <ul>
    <li>
      <a href="/home">Home</a>
    </li>
    <li>
      <a href="/about">About Us</a>
    </li>
    <li>
      <a href="/membership">Membership Details</a>
    </li>
    <li>
      <a href="/facilities">Facilities</a>
    </li>
    <li>
      <a href="/faq">FAQ</a>
    </li>
  </ul>
</nav>

我使用的是Java 1.8,JavaMail报告它是版本1.4.7

以下是会话调试的输出:

package testingstuff;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import javax.activation.*;
public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello World");


    final String user = "my work ID";
    final String pw = "my work Password";

    Properties props = new Properties(); 



    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "server I got from our outlook app");
    props.put("mail.smtp.port", "587");   

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(user, pw);
              }
            });

    session.setDebug(true);

    try {
        Message msg = new MimeMessage(session);
        String from = "kevin.camacho@company.com";
        String to = "my.colleague@company.com";
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(RecipientType.TO, new InternetAddress(to));
        msg.setSubject("TEST");
        msg.setText("TESTING");

        //Transport trans = session.getTransport("smtp");
        //trans.connect("smtp-mail.outlook.com", 25, user, pw);
        Transport.send(msg);
    }
    catch (Exception e) {
        System.out.println(e);
    }

    System.out.println("done");
}

}

嵌套异常是:

Hello World
DEBUG: setDebug: JavaMail version 1.4.7

DEBUG: getProvider() returning   javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "[my work email server]", port 587, isSSL false

javax.mail.MessagingException: Could not connect to SMTP host: [my work email server], port: 587;

完成

我使用的电子邮件服务器来自outlook应用程序。我进入帐户设置,选择了我的帐户并选择了更改。这将打开一个包含电子邮件配置的窗口,其中包含服务器地址。

2 个答案:

答案 0 :(得分:0)

尝试下面的代码,只需输入用户名,密码,来自主机

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   public static void main(String [] args)
   {    
          String username = "";
          String password = "";
          String to = "";
          String from = "";
          String host = "";

          Properties properties = System.getProperties();


          properties.setProperty("mail.smtp.host", host);
          properties.setProperty("mail.smtp.user", username);
          properties.setProperty("mail.smtp.password", password);
          properties.setProperty("mail.smtps.auth", "true"); 
          Session session = Session.getDefaultInstance(properties);

          try{
             MimeMessage message = new MimeMessage(session);

             message.setFrom(new InternetAddress(from));

             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

             message.setSubject("This is the Subject Line!");

             BodyPart messageBodyPart = new MimeBodyPart();

             messageBodyPart.setText("This is message body");

             Multipart multipart = new MimeMultipart();

             multipart.addBodyPart(messageBodyPart);




             message.setContent(multipart);



             message.saveChanges();
             Transport transport = session.getTransport("smtp");
             transport.connect(host, username, password);
             transport.sendMessage(message, message.getAllRecipients());
             transport.close();
             System.out.println("Sent message successfully....");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }
}



}

答案 1 :(得分:0)

问题最终导致我的主人错了。