📄 smtpwconfig.java
字号:
package jws.awt.net;
// Copyright 1997, John Webster Small
// All rights Reserved
import jws.net.*;
import jws.awt.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
public class SMTPwConfig extends SMTP
{
public SMTPwConfig()
{ super(); }
public SMTPwConfig
(String server, int port,
String senderHost, String senderEmailAddress)
{
super(server,port,senderHost,senderEmailAddress);
}
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)
{
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);
}
}
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);
Panel p = new WinPanel()
{
public void apply()
{
set(tfServer.getText(),
Integer.parseInt(tfPort.getText()),
tfSenderEmailAddress.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)); }
}
);
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
{
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 + -