📄 smtpwconfig.java
字号:
package Email.awt.net;
import Email.net.*;
import Email.awt.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
//smtp服务器注册窗口
public class SMTPwConfig extends SMTP {
public SMTPwConfig() {
super();
}
public SMTPwConfig (String server, int port, String senderHost, String senderEmailAddress,String User,String Pass) {
super(server,port,senderHost,senderEmailAddress,User,Pass);
}
public void setSenderEmailAddress( String sea ) {
senderEmailAddress = sea;
}
public String getSenderEmailAddress(Frame parent) {
if ( senderEmailAddress == null || senderEmailAddress.length() == 0 ) {
final WinDialog wd = new WinDialog(parent,"Your Email Address:",true);
final TextField addr = new TextField(30);
InsetPanel ip = new InsetPanel(10,10,10,10);
ip.add(addr);
wd.add("Center",ip);
Panel p = new Panel();
final Button apply = new Button("Apply");
final Button cancel = new Button("Cancel");
p.add(apply);
p.add(cancel);
wd.add("South",p);
apply.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sea = addr.getText();
if ( sea == null | sea.length() == 0 )
wd.dispose();
else {
setSenderEmailAddress(sea);
wd.dispose(true);
}
}
}
);
cancel.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
wd.dispose();
}
}
);
wd.pack();
wd.setVisible(true);
if ( !wd.ok )
senderEmailAddress = "";
}
return senderEmailAddress;
}
public void set (String server, int port, String senderEmailAddress,String user,String password) {
this.server = server;
this.port = port;
this.senderEmailAddress = senderEmailAddress;
try {
senderHost = senderEmailAddress.substring
(senderEmailAddress.indexOf("@")+1);
}
catch (Exception e) {
new WinFrame("Invalid email address",e);
}
this.user = user;
this.password = password;
}
public Panel configurePanel() {
final TextField tfServer = new TextField( server,30 );
final TextField tfPort = new TextField( String.valueOf(port),6 );
final TextField tfSenderEmailAddress = new TextField( senderEmailAddress,30 );
//7.4修改
final TextField tfUser = new TextField(user,30);
final TextField tfPassword = new TextField(30);
tfPassword.setEchoChar('*');
tfPassword.setText(password);
//以上定义发送邮件邮箱用户名和密码
Panel p = new WinPanel() {
public void apply() {
set(tfServer.getText(), Integer.parseInt( tfPort.getText() ), tfSenderEmailAddress.getText(),tfUser.getText(),tfPassword.getText() );
}
};
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
p.setLayout(gridBag);
Label l;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(1,2,1,2);
gridBag.setConstraints(l = new Label("Server: "), c);
p.add(l);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
gridBag.setConstraints(tfServer,c);
p.add(tfServer);
c.gridwidth = 1;
c.weightx = 0.0;
gridBag.setConstraints( l = new Label("Port: "), c );
p.add(l);
c.gridwidth = GridBagConstraints.RELATIVE;
gridBag.setConstraints(tfPort,c);
p.add(tfPort);
Button defaultPortButton = new Button("Default");
c.gridwidth = GridBagConstraints.REMAINDER;
gridBag.setConstraints(defaultPortButton,c);
p.add(defaultPortButton);
defaultPortButton.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfPort.setText( String.valueOf(defaultPort) );
}
}
);
//7.4 amending.Add user name and password
c.gridwidth = 1;
gridBag.setConstraints( l = new Label("User: "), c );
p.add(l);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.gridy = GridBagConstraints.RELATIVE;
gridBag.setConstraints(tfUser,c);
p.add( tfUser );
c.gridwidth = 1;
gridBag.setConstraints( l = new Label("Password: "), c );
p.add(l);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.gridy = GridBagConstraints.RELATIVE;
gridBag.setConstraints( tfPassword,c );
p.add( tfPassword );
//Email send address
c.gridwidth = 1;
gridBag.setConstraints(l = new Label("Email: "), c);
p.add(l);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.gridy = GridBagConstraints.RELATIVE;
gridBag.setConstraints(tfSenderEmailAddress,c);
p.add(tfSenderEmailAddress);
Panel ip = new InsetPanel(10,10,10,10);
Panel op = new OutlinedPanel("SMTP");
ip.add(op);
op.add(p);
ip.validate();
return ip;
}
public boolean configure(Frame parent) {
final WinDialog wd = new WinDialog(parent,"SMTP Configuration",true);
wd.add( "Center",configurePanel() );
Panel p = new Panel();
Button applyButton = new Button("Apply");
p.add(applyButton);
Button cancelButton = new Button("Cancel");
p.add(cancelButton);
wd.add("South",p);
wd.pack();
applyButton.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
wd.apply();
wd.dispose(true);
}
}
);
cancelButton.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
wd.dispose();
}
}
);
wd.setVisible(true);
return wd.ok;
}
public boolean sendMail(Frame parent, Enumeration messages) throws IOException, UnknownHostException {
//smtp服务器注册信息检验
if ( !messages.hasMoreElements() )
return false;
if ( server.length() == 0 || senderEmailAddress.length() == 0 )
if ( !configure(parent) )
return false;
if ( server.length() > 0 && senderEmailAddress.length() > 0 ) {
sendMail( messages );
return true;
}
return false;
}
/*
public static void main(String[] args)
{
WinFrame f = new WinFrame("SMTPwConfig Demo");
f.appMainWindow();
f.setVisible(true);
SMTPwConfig s = new SMTPwConfig();
s.configure(f);
f.dispose();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -