首页(162) xml(5) spring(1) 生活(8) java(70) 代码(10) 英语(4) 数据库(7) c#(14) 成长(10) 软件工程(27)  写新日志
 
 

玻璃杯中的花生壳

  真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。
   不要找任何理由,  当幸福在你身边的时候就抓住它,你就一定会很幸福! 
   

时 间 记 忆
«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

最 新 评 论
回复:xml的Jdom解析过程详解
回复:突然想到的几句话!
 Boyle came out of n
回复:xml的Jdom解析过程详解
回复:配置Spring数据源
回复:使用SAX解析XML
回复:java中写文件操作时FileOu
回复:关联和依赖关系的区分
回复:HttpSessionListen
回复:Spring AOP四种创建通知(

最 新 日 志
Java开发者的十大戒律
配置Spring数据源
java多线程设计模式
java中switch的使用
性格,编码,测试
突然想到的几句话!
理解Spring AOP中的关键概念
Spring AOP四种创建通知(拦截器
xml的四种解析方法 比较 sax,do
xml的Jdom解析过程详解

最 新 留 言
签写新留言

我渴望知识
很好的东东
帖子不错,道声谢
想拜师学艺
我的呼喊

搜 索


用 户 登 录
用户名称:
登陆密码:
密码保存:

友 情 连 接

模板设计:部落窝模板世界

blog名称:玻璃杯中的花生壳
日志总数:162
评论数量:249
留言数量:1
访问次数:828256
建立时间:2004年11月4日
 
 
 
[代码]用javamail发邮件(含附件)
[ 2006/9/4 17:40:02 | By: 玻璃杯中的花生壳 ]
 
//Title: Your Product Name//Version://Copyright: Copyright (c) 1999//Author: Your Name//Company: Your Company//Description: Your descriptionpackage com.mypackage;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class SendMail extends Frame {Label label1 = new Label();TextField textField1 = new TextField();Label label2 = new Label();TextField textField2 = new TextField();Label label3 = new Label();TextArea textArea1 = new TextArea();Label label4 = new Label();TextField textField3 = new TextField();Button button1 = new Button();Button button2 = new Button();public SendMail() {enableEvents(AWTEvent.WINDOW_EVENT_MASK);try {jbInit();}catch(Exception e) {e.printStackTrace();}}protected void processWindowEvent(WindowEvent e) {super.processWindowEvent(e);if(e.getID() == WindowEvent.WINDOW_CLOSING) {System.exit(0);}}public static void main(String[] args) {SendMail sendMail1 = new SendMail();sendMail1.setSize (400,400);sendMail1.show (true);}private void jbInit() throws Exception {label1.setBounds(new Rectangle(41, 38, 45, 23));label1.setText("收信人");this.setLayout(null);this.setSize (400,400);textField1.setBounds(new Rectangle(110, 36, 174, 23));label2.setBounds(new Rectangle(42, 75, 38, 23));label2.setText("主题");textField2.setBounds(new Rectangle(110, 76, 173, 23));label3.setBounds(new Rectangle(43, 148, 38, 23));label3.setText("内容");textArea1.setBounds(new Rectangle(110, 155, 256, 170));label4.setBounds(new Rectangle(45, 111, 44, 23));label4.setText("附件");textField3.setBounds(new Rectangle(110, 115, 173, 23));button1.setBounds(new Rectangle(70, 348, 88, 24));button1.setLabel("发送");button1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {button1_actionPerformed(e);}});button2.setBounds(new Rectangle(244, 348, 88, 24));button2.setLabel("重填");button2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {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);}void button2_actionPerformed(ActionEvent e) {textField1.setText ("");textField2.setText ("");textField3.setText ("");textArea1.setText ("");}void button1_actionPerformed(ActionEvent e) {String to,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;File file=new File(attachment);if(!attachment.equals ("")){if(!file.isFile ()){JOptionPane.showMessageDialog(this, "无效的附件名!", "错误", JOptionPane.ERROR_MESSAGE);return;}}//以上程序是检验输入的有效性// create some properties and get the default SessionProperties props = System.getProperties();props.put("mail.smtp.host", "192.168.0.1");Session session = Session.getDefaultInstance(props, null);session.setDebug(false);try{// create a messageMimeMessage msg = new MimeMessage(session);msg.setFrom(new InternetAddress(from));InternetAddress[] address = {new InternetAddress(to)};msg.setRecipients(Message.RecipientType.TO, address);msg.setSubject(subject);msg.addHeader ("toone","fangjianhua");if(attachment.equals ("")){System.out.println ("This is plain mail");msg.setText (message);}else {System.out.println ("this is a multipart mail");// create and fill the first message partMimeBodyPart mbp1 = new MimeBodyPart();mbp1.setText(message);// create the second message partMimeBodyPart mbp2 = new MimeBodyPart();// attach the file to the messageFileDataSource fds = new FileDataSource(file);mbp2.setDataHandler(new DataHandler(fds));mbp2.setFileName(fds.getName());// create the Multipart and its parts to itMultipart mp = new MimeMultipart();mp.addBodyPart(mbp1);mp.addBodyPart(mbp2);// add the Multipart to the messagemsg.setContent(mp);}msg.setSentDate(new Date());// send the message//for(int i=0;i<10;i++)Transport.send(msg);//System.out.println ("Send a mail success");JOptionPane.showMessageDialog(this, "邮件发送成功", "系统提示",JOptionPane.INFORMATION_MESSAGE );}catch(Exception ex){JOptionPane.showMessageDialog(this, "发送邮件失败", "错误", JOptionPane.ERROR_MESSAGE);}}}
 

阅读全文(2898) | 回复(1) | 编辑 | 精华
 
 
回复:用javamail发邮件(含附件)
[ 2006/9/5 17:12:58 | By: woziji(游客) ]
 
经过验证,可以的
 

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 
» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 
部落窝Blog模板世界部落窝Blog模板世界
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144781496 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号