发送附件的代码如下,使用的是struts,所以附件的上传是使用FormFile(mailForm
public void sendFile(MailForm mailForm,boolean debug) throws Exception
{
String from = mailForm.getFrom();
String to = mailForm.getTo();
String userName = mailForm.getUserName();
String password = mailForm.getPassword();
FormFile file = mailForm.getFile();
String content = mailForm.getContent();

String mailServer = this.getDomain(from);
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");

Session session = Session.getInstance(props);
if(debug)
{
session.setDebug(debug);
}

Message msg = new MimeMessage(session);
BodyPart filePart = new MimeBodyPart();
String fileName = mailForm.getFile().getFileName();
                  //这个ByteDataSource是自己实现的DataSource
ByteDataSource dataSource = new ByteDataSource(file);
filePart.setDataHandler(new DataHandler(dataSource));
filePart.setFileName(fileName);

Multipart mulPart = new MimeMultipart();
mulPart.addBodyPart(filePart);

filePart = new MimeBodyPart();
filePart.setContent(content, "text/html;charset=gb2312");
mulPart.addBodyPart(filePart);

msg.setContent(mulPart);
msg.setSubject(mailForm.getSubject());
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSentDate(new Date());
msg.saveChanges();

Transport transport = session.getTransport();
transport.connect(userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}

解决方案 »

  1.   

    控制台中显示的Debug如下:
    DEBUG: setDebug: JavaMail version 1.4.3
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.tom.com", port 25, isSSL false
    220 cnapp12.tom.com KBAS is ready
    DEBUG SMTP: connected to host "smtp.tom.com", port: 25EHLO 192.168.0.10
    250-cnapp42
    250-LININGPIPE
    250-AUTH LOGIN PLAIN
    250-AUTH=LOGIN PLAIN
    250 8BITMIME
    DEBUG SMTP: Found extension "LININGPIPE", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
    DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Attempt to authenticate
    DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
    AUTH LOGIN
    334 VXNlcm5hbWU6
    eGlhb21heWFucWk=
    334 UGFzc3dvcmQ6
    MzcyNDYzNTg=
    235 Authentication successful
    DEBUG SMTP: use8bit false
    MAIL FROM:<[email protected]>
    250 Ok
    RCPT TO:<[email protected]>
    250 Ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP:   [email protected]
    DATA
    354 Send from Rising mail proxy
    Date: Wed, 24 Feb 2010 16:21:29 +0800 (CST)
    From: [email protected]
    To: [email protected]
    Message-ID: <31196317.11.1266999689387.JavaMail.mayanqi@mayanqi-PC>
    Subject: subject
    MIME-Version: 1.0
    Content-Type: multipart/mixed; 
    boundary="----=_Part_10_16126503.1266999689382"------=_Part_10_16126503.1266999689382
    Content-Type: image/pjpeg; name=27474342.jpg
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename=27474342.jpg
    7O97kHn1x4cT0e8rrllC+E7g+1se8okhy6prDPCrGUalA6DoPf1wsUFalNRRmSS7HnYG1sW8yzSn
    iWGZGB/7TimoG5YSrgz7WtK06iYLGoJbSOl8B+IZo5IFgBsCbkg9MS5jWwKUaSVmmYljYG2FrNZ2
    N5S5ZQLm3kMZ6qSwjhPEX+K6o/Exwp4NO4Xyxt3CBZeEqBp2A1UybnkBbHO9XO1VVtM5/E17+mOg
    ZVWZjmf/2S8qICB8eEd2MDB8MTY0MzRjMWZhOGEwNzRiOGU4MGRkZWI2MTRkODk2YWYgKi8=
    ------=_Part_10_16126503.1266999689382
    Content-Type: text/html;charset=gb2312
    Content-Transfer-Encoding: 7bitffff
    ------=_Part_10_16126503.1266999689382--
    .
    250 QUIT
    DEBUG SMTP: EOF: [EOF]
      

  2.   

    我以前也试着用过javamail发邮件 ,但没有发送成功,  我也希望有高手来解决 !
      

  3.   

    java Mail 源代码http://www.itkanba.com/bbs/viewthread.php?tid=56&highlight=javamail
      

  4.   

    trying to connect to host "smtp.tom.com", port 25, isSSL false 
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();   // 设定 Mail Server
      senderImpl.setHost("------");   // SMTP验证时,需要用户名和密码
      senderImpl.setUsername("aaa");
      senderImpl.setPassword("bbb");
      // 不设这个是不能用用户名密码通过验证发的
      Properties prop = new Properties();
      prop.setProperty("mail.smtp.auth", "true");
      senderImpl.setJavaMailProperties(prop);
      // 建立邮件讯息
      MimeMessage mailMessage = senderImpl.createMimeMessage();
      MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
        true, "UTF-8");
      // 设定收件人、寄件人、主题与内文
      messageHelper.setTo("[email protected]");
      messageHelper.setFrom("[email protected]");
      messageHelper.setSubject("信息报告");
      messageHelper.setText("<html><head></head><body><h1>消息接收,请您查阅附件"
        + "</h1></body></html>", true);
      // 发附件,这里附件可以多个,只要再NEW一个FILE,再ADD一次,即可
      File files = new File(path+"\\"+"信息报送"+"_"+date+".xls");
      System.out.println("name="+files.getName());
     
      messageHelper.addAttachment(MimeUtility.encodeWord(files.getName()),
        files);
      // 传送邮件
      senderImpl.send(mailMessage);
      System.out.println("is ok");