如何在JAVA中连接SMTP服务器 你在论坛搜索javamail就能找到答案 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SMTP端口号:25POP3端口号:110用socket连接就可以了。好运! /*// header - edit "Data/yourJavaHeader" to customize// contents - edit "EventHandlers/Java file/onCreate" to customize//*/import java.awt.*;import java.awt.event.*;import java.util.*;import java.net.*;import java.io.*;import javax.swing.*;public class MailTest{ public static void main(String[] args) { JFrame frame =new MailTestFrame(); frame.show(); }}class MailTestFrame extends JFrame implements ActionListener{ public MailTestFrame() { setTitle("MailTest"); setSize(300,300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); getContentPane().setLayout(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints(); gbc.fill=GridBagConstraints.HORIZONTAL; gbc.weightx=0; gbc.weighty=0; gbc.weightx=0; add(new JLabel("From:"),gbc,0,0,1,1); gbc.weightx=100; from =new JTextField(20); add(from,gbc,1,0,1,1); gbc.weightx=0; add(new JLabel("To:"),gbc,0,1,1,1); gbc.weightx=100; to=new JTextField(20); add(to,gbc,1,1,1,1); gbc.weightx=0; add(new JLabel("SMTP server:"),gbc,0,2,1,1); gbc.weightx=100; smtpServer=new JTextField(20); add(smtpServer,gbc,1,2,1,1); gbc.fill=GridBagConstraints.BOTH; gbc.weighty=100; message=new JTextArea(); add(new JScrollPane(message),gbc,0,3,2,1); response=new JTextArea(); add(new JScrollPane (response),gbc,0,4,2,1); gbc.weighty=0; JButton sendButton=new JButton("Send"); sendButton.addActionListener(this); JPanel buttonPanel=new JPanel(); buttonPanel.add(sendButton); add(buttonPanel,gbc,0,5,2,1); } private void add(Component c,GridBagConstraints gbc,int x,int y,int w,int h) { gbc.gridx=x; gbc.gridy=y; gbc.gridwidth=w; gbc.gridheight=h; getContentPane().add(c,gbc); } public void actionPerformed(ActionEvent evt) { SwingUtilities.invokeLater(new Runnable() { public void run() { sendMail(); } }); } public void sendMail() { try { Socket s=new Socket(smtpServer.getText(),25); out =new PrintWriter(s.getOutputStream()); in=new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName=InetAddress.getLocalHost().getHostName(); send(null); send("Hello "+hostName); send("Mail FROM: "+from.getText()); send("RCPT TO: "+to.getText()); send("DATA"); out.println(message.getText()); send("."); s.close(); } catch(IOException exception) { response.append("Error: "+exception); } } public void send(String s) throws IOException { if (s!=null) { response.append(s+"_s\n"); out.println(s); out.flush(); } String line; //System.out.println(in.readLine()); if ((line=in.readLine())!=null) response.append(line+"_line\n"); } private BufferedReader in; private PrintWriter out; private JTextField from; private JTextField to; private JTextField smtpServer; private JTextArea message; private JTextArea response;} Properties props = System.getProperties();props.put("mail.smtp.host", host);props.put("mail.smtp.port", port);session = Session.getInstance(props);这样就建立了SMTP的Session会话 BufferedInputStream字节个数获取方法 JAVA如何实现音频的对比 一个小问题,大家讨论下. 一个超级变态的问题!!!解答下。。。 谁帮我解释一下这个查询语句? 我怎么老编译出错啊!大家来看看 怎样解压缩gz文件啊 我想取33到66之间的一个随机数,请问怎么取呀?在线等,比较急! 如果让一个JTable的一个单元格处于编辑状态呢? 关于static变量和实例变量的问题,求解! jni 调用 c 传入对象总不成功,很急!很急! 怎样使JTextField中的字符串居右对齐?
POP3端口号:110
用socket连接就可以了。好运!
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;public class MailTest
{ public static void main(String[] args)
{ JFrame frame =new MailTestFrame();
frame.show();
}
}class MailTestFrame extends JFrame implements ActionListener
{ public MailTestFrame()
{ setTitle("MailTest");
setSize(300,300);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=0;
gbc.weighty=0;
gbc.weightx=0;
add(new JLabel("From:"),gbc,0,0,1,1);
gbc.weightx=100;
from =new JTextField(20);
add(from,gbc,1,0,1,1);
gbc.weightx=0;
add(new JLabel("To:"),gbc,0,1,1,1);
gbc.weightx=100;
to=new JTextField(20);
add(to,gbc,1,1,1,1);
gbc.weightx=0;
add(new JLabel("SMTP server:"),gbc,0,2,1,1);
gbc.weightx=100;
smtpServer=new JTextField(20);
add(smtpServer,gbc,1,2,1,1);
gbc.fill=GridBagConstraints.BOTH;
gbc.weighty=100;
message=new JTextArea();
add(new JScrollPane(message),gbc,0,3,2,1);
response=new JTextArea();
add(new JScrollPane (response),gbc,0,4,2,1);
gbc.weighty=0;
JButton sendButton=new JButton("Send");
sendButton.addActionListener(this);
JPanel buttonPanel=new JPanel();
buttonPanel.add(sendButton);
add(buttonPanel,gbc,0,5,2,1);
}
private void add(Component c,GridBagConstraints gbc,int x,int y,int w,int h)
{ gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=w;
gbc.gridheight=h;
getContentPane().add(c,gbc);
}
public void actionPerformed(ActionEvent evt)
{ SwingUtilities.invokeLater(new Runnable()
{ public void run()
{ sendMail();
}
});
}
public void sendMail()
{ try
{ Socket s=new Socket(smtpServer.getText(),25);
out =new PrintWriter(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String hostName=InetAddress.getLocalHost().getHostName();
send(null);
send("Hello "+hostName);
send("Mail FROM: "+from.getText());
send("RCPT TO: "+to.getText());
send("DATA");
out.println(message.getText());
send(".");
s.close();
}
catch(IOException exception)
{ response.append("Error: "+exception);
}
}
public void send(String s) throws IOException
{ if (s!=null)
{ response.append(s+"_s\n");
out.println(s);
out.flush();
}
String line;
//System.out.println(in.readLine());
if ((line=in.readLine())!=null)
response.append(line+"_line\n");
}
private BufferedReader in;
private PrintWriter out;
private JTextField from;
private JTextField to;
private JTextField smtpServer;
private JTextArea message;
private JTextArea response;
}
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
session = Session.getInstance(props);
这样就建立了SMTP的Session会话