首页 > 解决方案 > JavaxException 未知 SMTP 主机异常:mail.gmail.com

问题描述

我正在尝试使用 Java 代码发送邮件。该代码在我的个人 PC 上运行时运行良好。但是在办公网络上出现了未知 SMTP 主机的异常。我的办公室电脑也无法 ping smtp.gmail.com。PC防火墙也关闭了。有没有其他方法可以建立连接?我还在下面提供我的代码以供参考。

mport javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;

public class otp {    
    String d_email = "email@gmail.com",
            d_password = "password",
            d_uname="uname",//your email password
            d_host = "mail.outlook.com",
            d_port = "587",
            m_to = "target@gmail.com", // Target email address
            m_subject = "Testing Mail programs",
            m_text = "Hey, this is a test email.";

    public otp() {
        Properties props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        try {
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props,auth); 
            session.setDebug(true);
            MimeMessage msg = new MimeMessage(session);
            msg.setText(m_text);
            msg.setSubject(m_subject);
            System.out.println(1);
            msg.setFrom(new InternetAddress(d_email));

            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

            System.out.println(3);
            Transport transport = session.getTransport("smtp");
            transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        Transport.send(msg);
        System.out.println("Message Sent succesfully");  
      } catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        otp blah = new otp();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
     public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(d_email, d_password);
        }
    }
}

标签: javajspide

解决方案


您使用的是 gmail 帐户,但您提供了 Outlook 主机,请尝试以下模板:

import java.util.Properties;

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 SendEmailUsingGMailSMTP {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "xyz@gmail.com";//change accordingly

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

      // >> gmail host
      String host = "smtp.gmail.com";

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "587");

      // Get the Session object.
      Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
         }
      });

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

         // Now set the actual message
         message.setText("Hello, this is sample for to check send "
            + "email using JavaMailAPI ");

         // Send message
         Transport.send(message);

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

      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }enter code here
   }
}

推荐阅读