import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;class Auth extends Authenticator{
    
private String s="",p="";
public Auth(String s,String p){
  this.s=s;
  this.p=p;
}

public  PasswordAuthentication getPasswordAuthentication(){
   return new PasswordAuthentication(s,p);
}
}
public class AttachExample {
   public static void main (String args[]) 
         throws Exception {
  
  String host = args[0];
      String from = args[1];
      String to = args[2];
  
      String fileAttachment = args[3];      // Get system properties
      Properties props = System.getProperties();      // Setup mail server
  
      props.put("mail.smtp.host", host);
  props.put("mail.smtp.auth", "true");      // Get session
      Session session = 
         Session.getInstance(props, new Auth(args[4],args[5]));   session.setDebug(true);
      // Define message
      MimeMessage message = 
         new MimeMessage(session);
  
      message.setFrom(
         new InternetAddress(from));
  
      message.addRecipient(
         Message.RecipientType.TO, 
         new InternetAddress(to));
  
      message.setSubject(
         "Hello JavaMail Attachment");      // create the message part 
      MimeBodyPart messageBodyPart = 
         new MimeBodyPart();      //fill message
      messageBodyPart.setText("Hi");      Multipart multipart = new MimeMultipart();
  
      multipart.addBodyPart(messageBodyPart);      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
  
      DataSource source = 
         new FileDataSource(fileAttachment);
      
  messageBodyPart.setDataHandler(
         new DataHandler(source));
      
  messageBodyPart.setFileName(fileAttachment);
      
  multipart.addBodyPart(messageBodyPart);      // Put parts in message
      message.setContent(multipart);      // Send the message
      Transport.send( message );
   }
}   参数1 邮件服务器地址
参数2 邮件来源
参数3 邮件目的地地址
参数4 附件文件本地地址
参数5 smtp服务器验证的用户名
参数6 smtp服务器验证的密码你可以根据自己的需要来修改程序!祝你好运!

解决方案 »

  1.   

    http://www.csdn.net/develop/Read_Article.asp?Id=14929
      

  2.   

    wks9527(空值异常):
    你说的对,建议在使用
    messageBodyPart.setFileName(fileAttachment);
    先把fileAttachment字符,转换成iso8859-1字符也许会好一点祝你好运!
      

  3.   

    这样写就可以了:
    messageBodyPart.setFileName(new String(fileAttachment.getBytes(),"ISO8859-1"));
      

  4.   

    import java.awt.*;
    import java.awt.event.*;public class mailSendFrame extends Frame {
      smtpMail mailSender=new smtpMail();
      Panel panelMain = new Panel();
      Panel panelUp = new Panel();
      Panel panel3 = new Panel();
      Panel panel4 = new Panel();
      Panel panel6 = new Panel();
      Panel panel7 = new Panel();
      Panel panel5 = new Panel();
      TextField txtServer = new TextField();
      Checkbox cboSendConf = new Checkbox();
      TextField txtUser = new TextField();  TextField txtPwd = new TextField();  TextField txtTo = new TextField();  TextField txtFrom = new TextField();  TextField txtSubject = new TextField();
      Panel panel8 = new Panel();  Label lblFile = new Label();
      Button cmdBrowse = new Button();
      Panel panelDown = new Panel();
      TextArea txtMail = new TextArea();
      Panel panel10 = new Panel();
      Button cmdSend = new Button();
      Button cmdExit = new Button();
      private FileDialog openFileDialog
    = new FileDialog(this,"打开文件",FileDialog.LOAD);  public mailSendFrame() {
        try {
          Init();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        mailSendFrame mailSendFrame = new mailSendFrame();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = mailSendFrame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        mailSendFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        mailSendFrame.setVisible(true);
        mailSendFrame.show();
      }
      private void Init() throws Exception {
        this.setLayout(new BorderLayout());
        panelMain.setLayout(new GridLayout(2,1));
        panelUp.setLayout(new GridLayout(6,1));
        panel3.setLayout(new FlowLayout());
        txtServer.setColumns(10);
        cboSendConf.setLabel("需要发送认证");
        cboSendConf.addItemListener(new java.awt.event.ItemListener() {
          public void itemStateChanged(ItemEvent e) {
            cboSendConf_itemStateChanged(e);
          }
        });
        panel5.setLayout(new FlowLayout());
        txtUser.setColumns(10);
        txtUser.setEnabled(false);
        txtUser.addTextListener(new java.awt.event.TextListener() {
          public void textValueChanged(TextEvent e) {
            txtUser_textValueChanged(e);
          }
        });
        txtPwd.setColumns(8);
        txtPwd.setEchoChar('*');
        txtPwd.setEnabled(false);
        this.setVisible(true);
        this.setBackground(Color.lightGray);
        this.setSize(new Dimension(400, 350));
        this.setTitle("邮件发送程序");
        this.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            this_windowClosing(e);
          }
        });
        txtTo.setColumns(10);
        txtFrom.setColumns(10);
        txtSubject.setColumns(10);
        lblFile.setText("              ");
        cmdBrowse.setLabel("浏览");
        cmdBrowse.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cmdBrowse_actionPerformed(e);
          }
        });
        panelDown.setLayout(new BorderLayout());
        txtMail.setColumns(20);
        panel10.setLayout(new FlowLayout());
        cmdSend.setLabel("发送");
        cmdSend.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cmdSend_actionPerformed(e);
          }
        });
        cmdExit.setLabel("退出");
        cmdExit.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cmdExit_actionPerformed(e);
          }
        });
        this.add(panelMain, BorderLayout.CENTER);
        panelMain.add(panelUp, null);
        panelUp.add(panel3, null);
        panel3.add(new Label("发信服务器:"), null);
        panel3.add(txtServer, null);
        panelUp.add(panel5, null);
        panelUp.add(panel4, null);
        panel4.add(new Label("收件人:"), null);
        panel4.add(txtTo, null);
        panelUp.add(panel6, null);
        panelUp.add(panel7, null);
        panel7.add(new Label("主题:"), null);
        panel7.add(txtSubject, null);
        panel3.add(cboSendConf, null);
        panel5.add(new Label("用户名:"), null);
        panel5.add(txtUser, null);
        panel5.add(new Label("口令:"), null);
        panel5.add(txtPwd, null);
        panel6.add(new Label("发件人:"), null);
        panel6.add(txtFrom, null);
        panelUp.add(panel8, null);
        panel8.add(new Label("附件:  "), null);
        panel8.add(lblFile, null);
        panel8.add(cmdBrowse, null);
        panelMain.add(panelDown, null);
        panelDown.add(txtMail, BorderLayout.CENTER);
        panelDown.add(panel10,  BorderLayout.SOUTH);
        panel10.add(cmdSend, null);
        panel10.add(cmdExit, null);
        panelDown.add(new Label("  "),  BorderLayout.EAST);
        panelDown.add(new Label("  "),  BorderLayout.WEST);
      }  void cmdExit_actionPerformed(ActionEvent e) {
        System.exit(0);
      }  void cboSendConf_itemStateChanged(ItemEvent e) {
        txtUser.setEnabled(cboSendConf.getState());
        txtPwd.setEnabled(cboSendConf.getState());
      }  void this_windowClosing(WindowEvent e) {
        System.exit(0);
      }  void cmdBrowse_actionPerformed(ActionEvent e) {
        openFileDialog.show();
        String fileName="";
        fileName= openFileDialog.getDirectory()+openFileDialog.getFile();
       if(!fileName.trim().equals(""))  lblFile.setText(fileName);
      }  void cmdSend_actionPerformed(ActionEvent e) {
        mailSender.setSendConf(cboSendConf.getState());
        if(cboSendConf.getState()){
           mailSender.setUser(txtUser.getText().trim());
           mailSender.setPwd(txtPwd.getText().trim());
        }
        mailSender.setDomain(txtServer.getText().trim());
        mailSender.setFrom(txtFrom.getText().trim());
        mailSender.setTo(txtTo.getText().trim());
        mailSender.addHeader("Subject",txtSubject.getText().trim()) ;
        mailSender.addData(txtMail.getText()) ;
        if(!lblFile.getText().trim().equals("") )
           mailSender.addAttachment(lblFile.getText().trim());
        mailSender.open(txtServer.getText().trim(),25);
        mailSender.transmit();
        mailSender.close();
      }  void txtUser_textValueChanged(TextEvent e) {
        txtFrom.setText(txtUser.getText());
      }
    }
      

  5.   

    Responsibilities: Activities as a Project Leader, included:
    • Analysis and Design.
    • Development/Modification/Enhancement of Java components.
    • Understanding client request and developing strategies to resolve them.
    • Coding web pages and Java Components.
    • Develop, Renovate and Review the code based on client request.
    • Execution of Functionality testing.
    • Resolving testing issues with the client.Project details: The project involves solving the queries or issues faced by a normal user or contractor. Also to provide additional feature requested by a user or contractor.
      

  6.   

    The OPIT project is to develop a new web-based mechanism that will provide high visibility to everyone associated with the process of obsolete part inventory reduction. The following is the summary of the requirements in scope:
    • Allow the Material controller all the information necessary to develop and execute strategies to manage obsolete part inventory dollars.
    • Minimize all non-value-added activities associated with data capture, additional research and analysis previously necessary to attack problem situations.
      

  7.   

    import java.util.*;
    import javax.mail.*;
    import java.io.*;
    import javax.mail.internet.*;
    import javax.activation.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class Email {
        public Email() {
            super();
        }
        public void sendmail(String smtp, String from, String to, String subject, String body){
            Session session;
            MimeMessage message;
            Properties props = System.getProperties();
            props.put("mail.smtp.host", smtp);
            session = Session.getInstance(props, null);
            message = new MimeMessage(session);
            try{
                message.setFrom(new InternetAddress(from));
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(subject);
                message.setText(body);
                Transport transport = session.getTransport("smtp");
                transport.send(message);
                System.out.println("send successfully");
            }catch(MessagingException e){
                e.getMessage();
            }
        }}