欢迎光临
我们一直在努力

用javamail发邮件(含附件),用jBuilder3实现-JSP教程,邮件相关

建站超值云服务器,限时71元/月

//title:        your product name
//version:      
//copyright:    copyright (c) 1999
//author:       your name
//company:      your company
//description:  your description

package mail;

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 session
    properties props = system.getproperties();
    props.put("mail.smtp.host", "192.168.0.1");
    session session = session.getdefaultinstance(props, null);
    session.setdebug(false);

  try{
   // create a message
        mimemessage 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 part
          mimebodypart mbp1 = new mimebodypart();
          mbp1.settext(message);

        // create the second message part
          mimebodypart mbp2 = new mimebodypart();

     // attach the file to the message
        filedatasource fds = new filedatasource(file);
       mbp2.setdatahandler(new datahandler(fds));
       mbp2.setfilename(fds.getname());

        // create the multipart and its parts to it
        multipart mp = new mimemultipart();
        mp.addbodypart(mbp1);
        mp.addbodypart(mbp2);

        // add the multipart to the message
        msg.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);
     }

  }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用javamail发邮件(含附件),用jBuilder3实现-JSP教程,邮件相关
分享到: 更多 (0)

相关推荐

  • 暂无文章