我正在尝试使用java邮件API发送邮件。这是我的代码
public static void sendMail(MailObject mail) throws Exception
{
String to = mail.toAdress;
String from = mail.fromName;
String host = "mail.mycompanyname.com"; //$NON-NLS-1$
// String port="25";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host); //$NON-NLS-1$
// properties.setProperty()); //$NON-NLS-1$ //$NON-NLS-2$
properties.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(properties);
// MimeMessage message = new MimeMessage(session);
String add = mail.attachmentLocation;
MimeMessage message = new MimeMessage(session);
message.setSubject(mail.subject);
// create the message
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message to be sent
messageBodyPart.setText(mail.message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//attach your file here
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(mail.attachmentLocation); // your file
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName(mail.attachmentName);
multipart.addBodyPart(messageBodyPart);
// Put message in parts
message.setContent(multipart);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
Transport.send(message);
System.out.println("Sent message successfully...."); //$NON-NLS-1$
}
class MailObject
{
String toAdress;
String fromName;
String subject;
String message;
String attachmentLocation;
String attachmentName;
HTMLPage pageObject;
}
但我得到无法连接到smtp服务器错误。这是完整的堆栈跟踪
javax.mail.MessagingException: Could not connect to SMTP host: mail.mycompany.com, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
Could not connect to SMTP host: mail.mycompanyname.com, port: 25
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at MailManager.sendMail(MailManager.java:68)
at MailManager$1.widgetSelected(MailManager.java:122)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at MailManager.openMailDialogue(MailManager.java:165)
at App2$2.widgetSelected(App2.java:311)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at App2.main(App2.java:521)
Caused by: java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 23 more
我尝试使用telnet ping服务器,它显示了欢迎屏幕。