|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
而学习JAVA我觉得最应该避免的就是:只学习,不思考,只记忆,不实践!发邮件//Title:YourProductName
//Version:
//Copyright:Copyright(c)1999
//Author:YourName
//Company:YourCompany
//Description:Yourdescription
packageMail;
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.util.*;
importjava.io.*;
importjavax.mail.*;
importjavax.mail.internet.*;
importjavax.activation.*;
publicclassSendMailextendsFrame{
Labellabel1=newLabel();
TextFieldtextField1=newTextField();
Labellabel2=newLabel();
TextFieldtextField2=newTextField();
Labellabel3=newLabel();
TextAreatextArea1=newTextArea();
Labellabel4=newLabel();
TextFieldtextField3=newTextField();
Buttonbutton1=newButton();
Buttonbutton2=newButton();
publicSendMail(){
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try{
jbInit();
}
catch(Exceptione){
e.printStackTrace();
}
}
protectedvoidprocessWindowEvent(WindowEvente){
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
publicstaticvoidmain(String[]args){
SendMailsendMail1=newSendMail();
sendMail1.setSize(400,400);
sendMail1.show(true);
}
privatevoidjbInit()throwsException{
label1.setBounds(newRectangle(41,38,45,23));
label1.setText("收信人");
this.setLayout(null);
this.setSize(400,400);
textField1.setBounds(newRectangle(110,36,174,23));
label2.setBounds(newRectangle(42,75,38,23));
label2.setText("主题");
textField2.setBounds(newRectangle(110,76,173,23));
label3.setBounds(newRectangle(43,148,38,23));
label3.setText("内容");
textArea1.setBounds(newRectangle(110,155,256,170));
label4.setBounds(newRectangle(45,111,44,23));
label4.setText("附件");
textField3.setBounds(newRectangle(110,115,173,23));
button1.setBounds(newRectangle(70,348,88,24));
button1.setLabel("发送");
button1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
button1_actionPerformed(e);
}
});
button2.setBounds(newRectangle(244,348,88,24));
button2.setLabel("重填");
button2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
button2_actionPerformed(e);
}
});
this.add(label1,null);
this.add(textField1,null);
this.add(textField2,null);
this.add(textField3,null);
this.add(textArea1,null);
this.add(label2,null);
this.add(label4,null);
this.add(label3,null);
this.add(button2,null);
this.add(button1,null);
}
voidbutton2_actionPerformed(ActionEvente){
textField1.setText("");
textField2.setText("");
textField3.setText("");
textArea1.setText("");
}
voidbutton1_actionPerformed(ActionEvente){
Stringto,from,subject,message,attachment;
from="toone@mail.com";
to=textField1.getText();
if(to.trim().equals("")){
JOptionPane.showMessageDialog(this,"收信人不克不及为空!","毛病",JOptionPane.ERROR_MESSAGE);
return;
}
message=textArea1.getText();
attachment=textField3.getText();
if(message.trim().equals("")&&attachment.trim().equals("")){
JOptionPane.showMessageDialog(this,"内容和附件不克不及都为空!","毛病",JOptionPane.ERROR_MESSAGE);
return;
}
if(to.indexOf("@")==-1){
JOptionPane.showMessageDialog(this,"有效的收信人地点!","毛病",JOptionPane.ERROR_MESSAGE);
return;
}
subject=textField2.getText().trim();
if(subject.equals(""))
if(JOptionPane.showConfirmDialog(this,"你不必要设置主题吗?","体系提醒",0)!=0)
return;
Filefile=newFile(attachment);
if(!attachment.equals("")){
if(!file.isFile()){
JOptionPane.showMessageDialog(this,"有效的附件名!","毛病",JOptionPane.ERROR_MESSAGE);
return;
}
}
//以上程序是查验输出的无效性
//createsomepropertiesandgetthedefaultSession
Propertiesprops=System.getProperties();
props.put("mail.smtp.host","192.168.0.1");
Sessionsession=Session.getDefaultInstance(props,null);
session.setDebug(false);
try{
//createamessage
MimeMessagemsg=newMimeMessage(session);
msg.setFrom(newInternetAddress(from));
InternetAddress[]address={newInternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.addHeader("toone","fangjianhua");
if(attachment.equals("")){
System.out.println("Thisisplainmail");
msg.setText(message);
}
else{
System.out.println("thisisamultipartmail");
//createandfillthefirstmessagepart
MimeBodyPartmbp1=newMimeBodyPart();
mbp1.setText(message);
//createthesecondmessagepart
MimeBodyPartmbp2=newMimeBodyPart();
//attachthefiletothemessage
FileDataSourcefds=newFileDataSource(file);
mbp2.setDataHandler(newDataHandler(fds));
mbp2.setFileName(fds.getName());
//createtheMultipartanditspartstoit
Multipartmp=newMimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
//addtheMultiparttothemessage
msg.setContent(mp);
}
msg.setSentDate(newDate());
//sendthemessage
//for(inti=0;i<10;i++)
Transport.send(msg);
//System.out.println("Sendamailsuccess");
JOptionPane.showMessageDialog(this,"邮件发送乐成","体系提醒",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exceptionex){
JOptionPane.showMessageDialog(this,"发送邮件失利","毛病",JOptionPane.ERROR_MESSAGE);
}
}
}
其实你不用Struts,spring这些工具,直接用jsp,servlet能够很方便地写出来,而且,可以根据个人的水平、爱好,有很多方案。而struts,spring这些工具的出来。 |
|