简体   繁体   English

JavaMail:无法连接到SMTP服务器

[英]JavaMail: Could not connect to SMTP server

The following code causes an error. 以下代码导致错误。 Please help me understand what's wrong. 请帮助我了解问题所在。

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
  public static void main(String [] args)throws MessagingException
  {
    SendMail sm=new SendMail();
     sm.postMail(new String[]{"abc@yahoo.co.in"},"hi","hello","xyz@gmail.com");
   }

public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "webmail.emailmyname.com");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
}

Exception:
<pre>
com.sun.mail.smtp.SMTPSendFailedException: 450 smtpout04.dca.untd.com Authentication required

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at SendMail.postMail(SendMail.java:52)
    at SendMail.main(SendMail.java:10)

The "Authentication required" in the exception message suggests that the target SMTP server requires you to log in (Perhaps via TLS or SSL). 异常消息中的“需要身份验证”表明目标SMTP服务器要求您登录(也许通过TLS或SSL)。 This wasn't common on SMTP servers until a few years ago (it's an anti-spam measure) so it's easy to overlook. 直到几年前,这在SMTP服务器上才很常见(这是一种反垃圾邮件措施),因此很容易忽略。

To authenticate with JavaMail : 使用JavaMail进行身份验证

To use SMTP authentication you'll need to set the mail.smtp.auth property (see below) or provide the SMTP Transport with a username and password when connecting to the SMTP server. 要使用SMTP身份验证,您需要设置mail.smtp.auth属性(请参见下文),或在连接到SMTP服务器时为SMTP传输提供用户名和密码。 You can do this using one of the following approaches: 您可以使用以下方法之一来执行此操作:

  • Provide an Authenticator object when creating your mail Session and provide the username and password information during the Authenticator callback. 在创建邮件会话时提供一个Authenticator对象,并在Authenticator回调过程中提供用户名和密码信息。

    Note that the mail.smtp.user property can be set to provide a default username for the callback, but the password will still need to be supplied explicitly. 请注意,可以将mail.smtp.user属性设置为提供回调的默认用户名,但是仍然需要显式提供密码。

    This approach allows you to use the static Transport send method to send messages. 这种方法允许您使用静态传输发送方法来发送消息。

  • Call the Transport connect method explicitly with username and password arguments. 使用用户名和密码参数显式调用Transport connect方法。

This approach requires you to explicitly manage a Transport object and use the Transport sendMessage method to send the message. 此方法要求您显式管理传输对象,并使用传输sendMessage方法发送消息。 The transport.java demo program demonstrates how to manage a Transport object. transport.java演示程序演示了如何管理Transport对象。 The following is roughly equivalent to the static Transport send method, but supplies the needed username and password: 以下内容大致等效于静态Transport send方法,但提供了所需的用户名和密码:

Transport tr = session.getTransport("smtp"); 运输tr = session.getTransport(“ smtp”);

tr.connect(smtphost, username, password); tr.connect(smtphost,用户名,密码);

msg.saveChanges(); msg.saveChanges(); // don't forget this //不要忘记这个

tr.sendMessage(msg, msg.getAllRecipients()); tr.sendMessage(msg,msg.getAllRecipients());

tr.close(); tr.close();

Note that many, many ISPs block access to external port 25 on servers outside of their network. 请注意,许多很多ISP阻止访问其网络外部服务器上的外部端口25。 Instead, the ISP forces you to use their SMTP server. 而是,ISP强制您使用其SMTP服务器。

If you're getting "authentication required", you must first enter your user name and password and issue at least one request such as check for new mail. 如果获得“需要身份验证”,则必须首先输入用户名和密码,并发出至少一个请求,例如检查新邮件。 Even though SMTP does not require a username and password to SEND email, many SMTP servers still implement this by making you log in and check your mail via POP or IMAP before you can send any. 即使SMTP不需要用户名和密码来发送电子邮件,许多SMTP服务器仍然可以通过使您登录并通过POP或IMAP检查邮件来实现此目的,然后再发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM