24小时热门版块排行榜    

查看: 202  |  回复: 1
当前主题已经存档。

bb135789

[交流] 【求助】java邮件发送软件开发的问题

最近在学习JAVA,在学习邮件开发时遇到了问题.运行程序后,出现N个邮件发送界面,且设置完成后,发送失败.小弟找不到原因,望哪位高手帮忙解答。小弟奉上所有金币。以下为所有程序:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;


public class UseSMTPMail extends JFrame {
   JButton sendButton=new JButton("发信";
   JLabel hostLabel=new JLabel("SMTP服务器";
   JLabel usernameLabel=new JLabel("用户名:";
   JLabel passwordLabel=new JLabel("密码:";
   JLabel fromLabel=new JLabel("发件人:";
   JLabel toLabel=new JLabel("收件人:";
   JLabel subjectLabel=new JLabel("主题:";
   JTextField usernameField=new JTextField(40);
   JPasswordField passwordField=new JPasswordField(40);
   JTextField hostField=new JTextField(40);
   JTextField fromField=new JTextField(40);
   JTextField toField=new JTextField(40);
   JTextField subjectField=new JTextField(40);
   JTextArea message=new JTextArea(10,10);
   JScrollPane jsp=new JScrollPane(message);
   public UseSMTPMail(){
     Container contentPane=this.getContentPane();
     JPanel labels=new JPanel(new GridLayout(6,1));
     JPanel fields=new JPanel(new GridLayout(6,1));
     labels.add(hostLabel);fields.add(hostField);
     labels.add(usernameLabel);fields.add(usernameField);
     labels.add(passwordLabel);fields.add(passwordField);
     labels.add(toLabel);fields.add(toField);
     labels.add(fromLabel);fields.add(toField);
     labels.add(subjectLabel);fields.add(subjectField);
     Box north=Box.createHorizontalBox();
     north.add(labels);north.add(fields);
     contentPane.add(north,"North";contentPane.add(jsp,"Center";
     JPanel south=new JPanel();
     south.add(sendButton);
     sendButton.addActionListener(new SendMail());
     contentPane.add(south,"South";
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setTitle("发送电子邮件";
     setSize(400,400);
     setVisible(true);
   }
   class SendMail implements ActionListener
   {
     public void actionPerformed(ActionEvent evt){
       try{
         Properties props=new Properties();
         props.put("mail.smtp.auth","true";
         Session session=Session.getDefaultInstance(props,new Authenticator(){
           public PasswordAuthentication getPasswordAuthentication(){
             return new PasswordAuthentication(usernameField.getText(),
             passwordField.getText());
          }
  });
  final Message msg=new MimeMessage(session);
  Address to=new InternetAddress(toField.getText());
  Address from=new InternetAddress(fromField.getText());
  msg.setFrom(from);
  msg.setRecipient(Message.RecipientType.TO,to);
  msg.setSentDate(new Date());
  msg.setSubject(transferChinese(subjectField.getText()));
  msg.setContent(message.getText(),"GBK";
  Runnable r=new Runnable(){
    public void run(){
      try{
      Transport.send(msg);
      }catch(Exception e){e.printStackTrace();}
    }
  };
  Thread t=new Thread(r);
  t.start();
  message.setText("";
  }catch(Exception e){e.printStackTrace();}
  }
   }
public String transferChinese(String strText){
try{
    strText=MimeUtility.encodeText(new String(strText.getBytes(),"GB2312","GB2312","B";
}catch(Exception e){e.printStackTrace();}
return strText;
}
public static void main(String[] args){
Font font=new Font("JFrame",Font.PLAIN,14);
    Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
    while(keys.hasMoreElements()){
    Object key=keys.nextElement();
    if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
    UseSMTPMail smail=new UseSMTPMail();
    }
}
}

召唤高手,高手快来啊~~~~~
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhaoxiaoqi

木虫 (著名写手)

★ ★
波不动(金币+2,VIP+0):感谢参与应助! 12-8 19:16
可以参照孙卫琴 的 JAVA网络编程
上面的解说还是比较详细的。
2楼2009-12-08 19:11:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 bb135789 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[公派出国] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ero8OE6tv9cu 2026-09-02 4/200 2026-09-03 03:14 by rM1TE0WVDIIY
[博后之家] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 ero8OE6tv9cu 2026-09-02 6/300 2026-09-02 23:59 by rM1TE0WVDIIY
[基金申请] 科研人应该花精力去思考如何解决问题,而不是去凝练问题 +7 瞬息宇宙 2026-09-01 14/700 2026-09-02 17:33 by ma0526
[基金申请] 要骂人了,新模版改版就是要淡化问题凝练这种虚的东西,结果有个评委还在说凝练得不够 +9 瞬息宇宙 2026-08-31 17/850 2026-09-02 14:04 by anjeeshine
[基金申请] 学科评审组评审是指会评吗? +5 瞬息宇宙 2026-08-31 5/250 2026-09-02 10:13 by 雨冰共舞
[基金申请] 面上合作单位盖章 +6 ssyjh 2026-08-27 9/450 2026-09-01 20:01 by huagongfeihu
[基金申请] 面上函评意见出来了,像什么等级? 20+4 Tsingking1 2026-08-27 17/850 2026-09-01 19:51 by 超级无敌华子
[论文投稿] 小白求助 投论文要求的highlights应该如何写 5+3 l1963982152 2026-08-29 4/200 2026-09-01 09:04 by 北京莱茵编辑
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +7 gdd2018 2026-08-28 12/600 2026-09-01 08:32 by 尼古拉斯小虫
[基金申请] 国社科又开始会评了,不知道这次命运如何 +7 雨打竹帘 2026-08-30 11/550 2026-08-31 23:16 by hittle2008
[基金申请] 面上意见出来了 +12 黄鸟于飞Chao 2026-08-29 23/1150 2026-08-31 18:57 by 黄鸟于飞Chao
[基金申请] 能否申诉? +7 echo8914667 2026-08-30 8/400 2026-08-31 17:00 by yihongxu
[基金申请] 中青基了要发朋友圈吗? +7 349506619 2026-08-28 7/350 2026-08-31 13:39 by 冼亮淀粉酶
[基金申请] 29号明天会评吗 +4 笨笨唐 2026-08-28 4/200 2026-08-31 09:30 by huixian257
[基金申请] 为什么到现在没收到通知? +5 tannykie 2026-08-29 5/250 2026-08-30 21:05 by purplejack
[基金申请] 有没有仍没收到信息的 +7 德尚中行 2026-08-27 8/400 2026-08-30 20:52 by purplejack
[考博] 找导师 +6 yuanjiabao 2026-08-29 7/350 2026-08-30 14:40 by 生科新手
[基金申请] 我就是申请一个面上项目而已,这评审意见是按照杰青的条件评的吧? +6 gouxfjh 2026-08-28 11/550 2026-08-30 07:57 by gouxfjh
[基金申请] 国自然评审意见 +13 wangmingqi 2026-08-28 19/950 2026-08-29 10:22 by Poppy1104
[基金申请] 2026年叶企孙基金 +4 bud_bud 2026-08-27 7/350 2026-08-29 07:23 by foolishmani
信息提示
请填处理意见