我正在尝试用 java 发送邮件,但我不断收到此错误“com.sun.mail.smtp.SMTPSendFailedException:550 未能满足 SPF 要求”,我已经搜索了互联网,看看是否有其他人遇到过这个问题java,一无所获。任何人都知道这个错误意味着什么?我发送电子邮件的代码如下。
//Create session and message
Properties props = System.getProperties();
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", mailhost);
javax.mail.Authenticator auth = null;
auth = new javax.mail.Authenticator() {
@Override
public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
};
session = Session.getInstance(props, auth);
Message msg = new MimeMessage(session);
//Set from,recipients,content and other stuff here
//...................
//Send the message
Transport.send(msg);
最佳答案
通过将“mail.smtp.auth”属性设置为 true 并添加属性“mail.smtp.ssl.enable”并将其设置为 true 然后最终而不是使用下面的静态方法发送消息。
Transport.send(Message msg)
我在从 session 对象获取的传输对象上使用实例方法来发送消息。
transport.sendMessage(Message msg, Address[] addresses)
下面是修改后的工作代码。
//Create session
Properties props = System.getProperties();
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.ssl.enable", "true");
javax.mail.Authenticator auth = null;
auth = new javax.mail.Authenticator() {
@Override
public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
};
session = Session.getInstance(props, auth);
//get transport object from session and connect to mail server
Transport tr = session.getTransport("smtp");
tr.connect(session.getProperty("mail.smtp.host"), session.getProperty("mail.smtp.user"), session.getProperty("mail.smtp.password"));
//create message and set from,recipients,content and other stuff here on the message object.
Message msg = new MimeMessage(session);
//..................
//...................
//Save and send the message
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
此链接确实帮助我解决了问题:http://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
关于java - 不能用Java发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189748/