gpt4 book ai didi

javax.mail.SendFailedException

转载 作者:行者123 更新时间:2023-11-30 11:23:17 25 4
gpt4 key购买 nike

请帮助解决以下异常:

DEBUG SMTP RCVD: 250 2.1.0 Sender OK

DEBUG SMTP SENT: RCPT TO:<XXXX@gmail.com>
DEBUG SMTP RCVD: 550 5.7.1 Unable to relay

Invalid Addresses
XXX@gmail.com
DEBUG SMTPTransport: Sending failed because of invalid destination addresses
DEBUG SMTP SENT: RSET
DEBUG SMTP RCVD: 250 2.0.0 Resetting

DEBUG SMTP SENT: QUIT

Exception in thread "main" javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:804)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:320)
at javax.mail.Transport.send0(Transport.java:151)
at javax.mail.Transport.send(Transport.java:80)
at StatsEmail.sendEmail(StatsEmail.java:95)
at Statements.main(Statements.java:73)

源代码/

    String host = "XXXXX";
String from = "XXXX";


java.util.Properties props = new java.util.Properties();


props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");

props.put("mail.smtp.port","25");


props.put("mail.smtp.debug", "true");

String[] to = {"XXXX@gmail.com"};

javax.mail.Session session = Session.getDefaultInstance(props, null);
javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session);

message.setFrom(new InternetAddress(from));

javax.mail.internet.InternetAddress[] toAddress = new javax.mail.internet.InternetAddress[to.length];


for(int i =0; i< to.length; i++ ){

toAddress[i] = new InternetAddress(to[i]);
}



for(int i=0; i<toAddress.length;i++){

message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}


message.setSubject("TestMail");
message.setText("TestJavaMail");

Transport.send(message);


System.out.println("Send Successful");

最佳答案

您的代码似乎可以访问任何其他服务器,但不能访问 gmail smtp 服务器。

如果您想使用 gmail 服务器,您需要对帐户进行身份验证。以下一段源代码可能会对您有所帮助。注意:我使用的是 javax.mail 版本 1.4.7

import java.util.Properties;
import javax.mail.Authenticator;
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 App {

public static void main(String[] args) {
String from = "XXXX@gmail.com";
String to = "XXXX@web.de";

Properties properties = createConfiguration();

SmtpAuthenticator authentication = new SmtpAuthenticator();

Session session = Session.getDefaultInstance(properties, authentication);

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

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

// Send message
Transport.send(message);

System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}

private static Properties createConfiguration() {
return new Properties() {
{
put("mail.smtp.auth", "true");
put("mail.smtp.host", "smtp.gmail.com");
put("mail.smtp.port", "587");
put("mail.smtp.starttls.enable", "true");
}
};
}

private static class SmtpAuthenticator extends Authenticator {

private String username = "XXXX@gmail.com";
private String password = "secret";

@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
}

关于javax.mail.SendFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294146/

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