📄 sendmail.java
字号:
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class SendMail extends Frame implements ActionListener{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Label email = new Label("收 信 人");
Label subject = new Label("邮件主题");
Label account = new Label("发信帐号");
Label content = new Label("信件内容");
Label attach = new Label("粘贴附件");
TextField emailfield = new TextField(30);
TextField subjectfield = new TextField("hello",30);
TextField attachfield = new TextField(30);
Choice accountchoice = new Choice();
Checkbox masscheck = new Checkbox("群发信件");
TextArea contentfield = new TextArea(10,65);
Button sendbutton = new Button("发 信");
Button resetbutton = new Button("重 写");
Button massbutton = new Button("群体邮件");
Button attachbutton = new Button("粘贴附件");
Listener listener = new Listener();
public SendMail(){
setTitle("新邮件");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
//邮件地址,群发选择
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1,1,1,1);
gbc.gridwidth = 1;
add(email,gbc);
add(emailfield,gbc);
masscheck.setState(false);
add(masscheck,gbc);
masscheck.addItemListener(listener);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(massbutton,gbc);
massbutton.setEnabled(false);
massbutton.setActionCommand("massbutton");
massbutton.addActionListener(this);
//邮件主题,发信帐号
gbc.gridwidth = 1;
add(subject,gbc);
add(subjectfield,gbc);
add(account,gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
File dir = new File("account/");
String [] listing = dir.list();
for(int i=0;i<listing.length;i++)
accountchoice.addItem(listing[i]);
//accountchoice.addItem("Surname");
//accountchoice.addItem("Secondname");
add(accountchoice,gbc);
//accountchoice.select(0);
//邮件附件
gbc.gridwidth = 1;
add(attach,gbc);
add(attachfield,gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(attachbutton,gbc);
attachbutton.setActionCommand("attachbutton");
attachbutton.addActionListener(this);
//邮件内容
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(content,gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 4;
add(contentfield,gbc);
//发信和取消按钮
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
add(sendbutton,gbc);
sendbutton.setActionCommand("sendbutton");
sendbutton.addActionListener(this);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(resetbutton,gbc);
resetbutton.setActionCommand("resetbutton");
resetbutton.addActionListener(this);
//pack();
setSize(485,324);
Dimension scrnSize = toolkit.getScreenSize();
Dimension frmSize = getSize();
System.out.println(frmSize.width + ":"+frmSize.height);
setLocation(scrnSize.width/2-frmSize.width/2,scrnSize.height/2-frmSize.height/2);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event){
dispose();
//System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("massbutton")){
FileDialog fd = new FileDialog(this,"打开邮件列表",FileDialog.LOAD);
fd.setFile("*.txt");
fd.show();
if(fd.getFile()!=null)
emailfield.setText(fd.getDirectory()+fd.getFile());
}else if(e.getActionCommand().equals("attachbutton")){
FileDialog fd = new FileDialog(this,"要增加的附件",FileDialog.LOAD);
fd.setFile("*.*");
fd.show();
if(fd.getFile()!=null)
attachfield.setText(fd.getDirectory()+fd.getFile());
}else if(e.getActionCommand().equals("resetbutton")){
this.setTitle("新邮件");
emailfield.setText("");
subjectfield.setText("");
attachfield.setText("");
masscheck.setState(false);
massbutton.setEnabled(false);
contentfield.setText("");
}else if(e.getActionCommand().equals("sendbutton")){
//发信
String fileline,getname,getparement;
int pos;
boolean ret;
SMTP sendsmtp = new SMTP();
this.setTitle("正在发信,稍侯......");
//读取帐号信息
try{
FileReader fis = new FileReader("account/"+accountchoice.getSelectedItem());
BufferedReader dis = new BufferedReader(fis);
while((fileline = dis.readLine())!=null){
this.setCursor(WAIT_CURSOR);
pos = GetPos(fileline);
if (pos>=0){
getname = fileline.substring(0,pos - 1);
getparement = fileline.substring(pos + 1,fileline.length());
if(getname.equalsIgnoreCase("email"))
sendsmtp.setFrom(getparement.trim());
else if(getname.equalsIgnoreCase("user"))
sendsmtp.setUser(getparement.trim());
else if(getname.equalsIgnoreCase("password"))
sendsmtp.setPass(getparement.trim());
else if(getname.equalsIgnoreCase("smtp"))
sendsmtp.setServer(getparement.trim());
}
}
fis.close();
}catch(IOException error){
System.out.println("File Open Error!");
}catch(Exception error){
System.out.println("Exception:" + error);
}
sendsmtp.setSubject(subjectfield.getText().trim());
sendsmtp.setBody(contentfield.getText().trim() + "\n\n ---《邮件群发1.0》 CopyRight to OUR Team ---");
sendsmtp.attach(attachfield.getText());
//读取邮件列表
if(masscheck.getState() == true){
try{
FileReader fis = new FileReader(emailfield.getText());
BufferedReader dis = new BufferedReader(fis);
//int i = 0;
//String ToString = new String();
while((fileline = dis.readLine())!=null){
//if(i != 0)
// ToString = ToString + ";" + fileline.trim();
//else{
// ToString = fileline.trim();
// this.setTitle(ToString);
//}
//if(((++i)%10)==0){
this.setCursor(WAIT_CURSOR);
//System.out.println(ToString);
sendsmtp.setTo(fileline.trim());
//sendsmtp.setTo(ToString);
ret = sendsmtp.sendMail();
this.setCursor(DEFAULT_CURSOR);
// i = 0;
// ToString = "";
//}
}
fis.close();
}catch(Exception error){
System.out.println("Exception:" + error);
}
}else{
sendsmtp.setTo(emailfield.getText().trim());
sendsmtp.setBcc("zhui@xiyou.edu.cn");
try{
ret = sendsmtp.sendMail();
}catch(Exception error){
//error.printStackTrace();
System.out.println("Exception:" + error);
}
}
//记录发信日志
try{
FileWriter logfile = new FileWriter("maillog.txt",true);
BufferedWriter log = new BufferedWriter(logfile);
log.write("["+System.currentTimeMillis()+"]");
log.newLine();
log.write("发信人:" + accountchoice.getSelectedItem());
log.write("\t收件人:" + emailfield.getText());
log.newLine();
log.write("主题:" + subjectfield.getText());
log.newLine();
log.write("邮件内容:\n" + contentfield.getText());
log.newLine();
log.close();
}catch(IOException error){
System.out.println("File Open Error!");
}catch(Exception error){
System.out.println("Exception:"+e);
}
this.setTitle("本次发信完成");
this.setCursor(DEFAULT_CURSOR);
}
}
class Listener implements ItemListener{
public void itemStateChanged(ItemEvent event){
emailfield.setText("");
if (masscheck.getState() == true)
massbutton.setEnabled(true);
else
massbutton.setEnabled(false);
}
}
//获取"="所在位置
static int GetPos(String iniString){
int pos;
pos = iniString.indexOf("=");
return(pos);
}
public static void main(String[] args){
SendMail demo = new SendMail();
demo.setVisible(true);
demo.setResizable(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -