代码如下:想向www.126.com发送一个邮件,所以我nslookup获得服务器的地址
GUI填写的信息如下--刚开始了解这方面的东西所以有错误请指教。
SMTP Server:ns3.nease.netFrom:[email protected]
To:[email protected]:Test JavaMial
以及发送的一些内容。放松后提示一个错误
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: ns.nease.net, port: 25, response: -1
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at ApplicationMial$1.run(ApplicationMial.java:183)
at java.lang.Thread.run(Thread.java:534)*/请高手指点Thank!
import javax.mail.*;import javax.mail.internet.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ApplicationMial extends JFrame { private JButton sendButton = new JButton("Send Message"); private JLabel fromLabel = new JLabel("From: "); private JLabel toLabel = new JLabel("To: "); private JLabel hostLabel = new JLabel("SMTP Server: "); private JLabel subjectLabel = new JLabel("Subject: "); private JTextField fromField = new JTextField(40); private JTextField toField = new JTextField(40); private JTextField hostField = new JTextField(40); private JTextField subjectField = new JTextField(40); private JTextArea message = new JTextArea(40, 72); private JScrollPane jsp = new JScrollPane(message); public ApplicationMial( ) { super("SMTP Client"); Container contentPane = this.getContentPane( ); contentPane.setLayout(new BorderLayout( )); JPanel labels = new JPanel( ); labels.setLayout(new GridLayout(4, 1)); labels.add(hostLabel); JPanel fields = new JPanel( ); fields.setLayout(new GridLayout(4, 1)); String host = System.getProperty("mail.host", ""); hostField.setText(host); fields.add(hostField); labels.add(toLabel); fields.add(toField); String from = System.getProperty("mail.from", ""); fromField.setText(from); labels.add(fromLabel); fields.add(fromField); labels.add(subjectLabel); fields.add(subjectField); Box north = Box.createHorizontalBox( ); north.add(labels); north.add(fields); contentPane.add(north, BorderLayout.NORTH); message.setFont(new Font("Monospaced", Font.PLAIN, 12)); contentPane.add(jsp, BorderLayout.CENTER); JPanel south = new JPanel( ); south.setLayout(new FlowLayout(FlowLayout.CENTER)); south.add(sendButton); sendButton.addActionListener(new SendAction( )); contentPane.add(south, BorderLayout.SOUTH); this.pack( ); } class SendAction implements ActionListener { public void actionPerformed(ActionEvent evt) { try { Properties props = new Properties( ); props.put("mail.host", hostField.getText( )); Session mailConnection = Session.getInstance(props, null); final Message msg = new MimeMessage(mailConnection); Address to = new InternetAddress(toField.getText( )); Address from = new InternetAddress(fromField.getText( )); msg.setContent(message.getText( ), "text/plain"); msg.setFrom(from); msg.setRecipient(Message.RecipientType.TO, to); msg.setSubject(subjectField.getText( )); Runnable r = new Runnable( ) { public void run( ) { try { Transport.send(msg); } catch (Exception ex) { ex.printStackTrace( ); } } }; Thread t = new Thread(r); t.start( ); message.setText(""); } catch (Exception ex) { ex.printStackTrace( ); } } } public static void main(String[] args) { ApplicationMial client = new ApplicationMial();
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.show();
}}
GUI填写的信息如下--刚开始了解这方面的东西所以有错误请指教。
SMTP Server:ns3.nease.netFrom:[email protected]
To:[email protected]:Test JavaMial
以及发送的一些内容。放松后提示一个错误
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: ns.nease.net, port: 25, response: -1
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at ApplicationMial$1.run(ApplicationMial.java:183)
at java.lang.Thread.run(Thread.java:534)*/请高手指点Thank!
import javax.mail.*;import javax.mail.internet.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ApplicationMial extends JFrame { private JButton sendButton = new JButton("Send Message"); private JLabel fromLabel = new JLabel("From: "); private JLabel toLabel = new JLabel("To: "); private JLabel hostLabel = new JLabel("SMTP Server: "); private JLabel subjectLabel = new JLabel("Subject: "); private JTextField fromField = new JTextField(40); private JTextField toField = new JTextField(40); private JTextField hostField = new JTextField(40); private JTextField subjectField = new JTextField(40); private JTextArea message = new JTextArea(40, 72); private JScrollPane jsp = new JScrollPane(message); public ApplicationMial( ) { super("SMTP Client"); Container contentPane = this.getContentPane( ); contentPane.setLayout(new BorderLayout( )); JPanel labels = new JPanel( ); labels.setLayout(new GridLayout(4, 1)); labels.add(hostLabel); JPanel fields = new JPanel( ); fields.setLayout(new GridLayout(4, 1)); String host = System.getProperty("mail.host", ""); hostField.setText(host); fields.add(hostField); labels.add(toLabel); fields.add(toField); String from = System.getProperty("mail.from", ""); fromField.setText(from); labels.add(fromLabel); fields.add(fromField); labels.add(subjectLabel); fields.add(subjectField); Box north = Box.createHorizontalBox( ); north.add(labels); north.add(fields); contentPane.add(north, BorderLayout.NORTH); message.setFont(new Font("Monospaced", Font.PLAIN, 12)); contentPane.add(jsp, BorderLayout.CENTER); JPanel south = new JPanel( ); south.setLayout(new FlowLayout(FlowLayout.CENTER)); south.add(sendButton); sendButton.addActionListener(new SendAction( )); contentPane.add(south, BorderLayout.SOUTH); this.pack( ); } class SendAction implements ActionListener { public void actionPerformed(ActionEvent evt) { try { Properties props = new Properties( ); props.put("mail.host", hostField.getText( )); Session mailConnection = Session.getInstance(props, null); final Message msg = new MimeMessage(mailConnection); Address to = new InternetAddress(toField.getText( )); Address from = new InternetAddress(fromField.getText( )); msg.setContent(message.getText( ), "text/plain"); msg.setFrom(from); msg.setRecipient(Message.RecipientType.TO, to); msg.setSubject(subjectField.getText( )); Runnable r = new Runnable( ) { public void run( ) { try { Transport.send(msg); } catch (Exception ex) { ex.printStackTrace( ); } } }; Thread t = new Thread(r); t.start( ); message.setText(""); } catch (Exception ex) { ex.printStackTrace( ); } } } public static void main(String[] args) { ApplicationMial client = new ApplicationMial();
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.show();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货