gpt4 book ai didi

Java邮件 : Could not connect to SMTP server

转载 作者:行者123 更新时间:2023-12-01 16:05:27 25 4
gpt4 key购买 nike

以下代码会导致错误。请帮助我理解出了什么问题。

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)

最佳答案

异常消息中的“需要身份验证”表明目标 SMTP 服务器要求您登录(可能通过 TLS 或 SSL)。直到几年前,这种情况在 SMTP 服务器上并不常见(这是一种反垃圾邮件措施),因此很容易被忽视。

authenticate with 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. 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.

    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.

    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.

This approach requires you to explicitly manage a Transport object and use the Transport sendMessage method to send the message. The transport.java demo program demonstrates how to manage a Transport object. The following is roughly equivalent to the static Transport send method, but supplies the needed username and password:

Transport tr = session.getTransport("smtp");

tr.connect(smtphost, username, password);

msg.saveChanges(); // don't forget this

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

tr.close();

关于Java邮件 : Could not connect to SMTP server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2724441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com