我正在尝试发送电子邮件,但出现以下错误:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: 10.17.1.65, port: 25
我正在尝试发送电子邮件,但出现以下错误:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: 10.17.1.65, port: 25
在这里,我分享了一个使用带有多个附件的 Gmail SSL 发送邮件的类。只需使用实际的 gmail 凭据更改 gmailUsername 和 gmailPass。并使用您想要的地址更改往返地址。在这里,我们使用 Gmail SSL smtp 端口发送邮件。您可以使用自己的邮件服务器详细信息对其进行更改。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
Author Mridul Chatterjee
*/
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 SendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put(”mail.smtp.host”, “smtp.gmail.com”);
props.put(”mail.smtp.socketFactory.port”, “465″);
props.put(”mail.smtp.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
props.put(”mail.smtp.auth”, “true”);
props.put(”mail.smtp.port”, “465″);
ArrayList fileNames = new ArrayList();
fileNames.add(”C:/Write.txt”);
fileNames.add(”C:/Write1.txt”);
fileNames.add(”C:/Write2.txt”);
fileNames.add(”C:/Write3.txt”);
fileNames.add(”C:/25148.jpg”);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(”gmailUsername”,”gmailPass”);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(”from@no-spam.com”));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(”tomail@mail.com”));
message.setSubject(”Testing Subject”);
message.setText(”Dear Mail Crawler,” +
“nn No spam to my email, please!”);
// multipart.addBodyPart(messageBodyPart);
// DataSource source = new FileDataSource(filename);
// messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
System.out.println(fileNames.size());
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
for(int i=0;i {
System.out.println(fileNames.get(i));
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource((String)fileNames.get(i));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName((String)fileNames.get(i));
multipart.addBodyPart(messageBodyPart);
//message.setContent(multipart);
}
//multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println(”Mail Sent Successfully….”);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
首先测试 netstat 命令是否主机响应。如果是,那么您需要检查主机上的电子邮件服务器/中继。您可以通过此链接http://www.mailradar.com/openrelay/对其进行测试