就是把一个excel文件同时发送出去,哪位做过的?

解决方案 »

  1.   

    增加附件...
    BodyPart bodyPart = new MimeBodyPart();
    FileDataSource fileDataSource = new FileDataSource(
    filename);
    bodyPart
    .setDataHandler(new DataHandler(fileDataSource));
    bodyPart.setFileName(fileDataSource.getName());
    multipart.addBodyPart(bodyPart);
    private Multipart multipart;
      

  2.   

    //目前并未达到需求,只是发送邮件,现在想假设d:/aa/text.excel有这个文件,把这个文件也发送出去。
    public class MailSendBean { public MailSendBean(){
    System.out.println("used struction fMailSend");

    }
    public void sendMail(){
    System.out.println("enter sendMail method");
    try{
    String host="mail.eastcom-sw.com";
    String from="[email protected]";
    String to="[email protected]";
     // Get system properties
        Properties props = System.getProperties();     // Setup mail server
        props.put("mail.smtp.host", host);
        
        //set auth
       // props.put( "mail.smtp.auth ", "true "); 
        Authenticator auth = new Authenticator();      // Get session
        Session session = Session.getDefaultInstance(props, null);     // Define message
        MimeMessage message = new MimeMessage(session);     // Set the from address
        message.setFrom(new InternetAddress(from));     // Set the to address
        message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to));     // Set the subject
        message.setSubject("Hello JavaMail");     // Set the content
        message.setText("Welcome to JavaMail");     // Send message
        Transport.send(message);
        
        System.out.println("successfully send mail");
        
    }catch(Exception e){
    e.printStackTrace();
    System.out.println(e.getMessage());
    }
    }
    public static void main(String arg[]){
    MailSendBean mailSendBean=new MailSendBean();
    mailSendBean.sendMail();
    }
    private class Authenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() 

    String un = "bxxyzj"; 
    String pw = "851004"; 
    return new PasswordAuthentication(un, pw); 
    }  }
    }
      

  3.   

    import java.util.Properties;import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;/**
     * @author jacky.zhu
     *
     * @createDate 2009-2-9
     *
     */
    public class MailSendBean { 

    private Multipart multipart;

    public MailSendBean(){ 
    System.out.println("used struction fMailSend");  } 
    public void sendMail(){ 
    System.out.println("enter sendMail method"); 
    try{ 
    String host="mail.eastcom-sw.com"; 
    String from="[email protected]"; 
    String to="[email protected]"; 
    //  Get system properties 
        Properties props = System.getProperties();      // Setup mail server 
        props.put("mail.smtp.host", host); 
        
        //set auth 
      // props.put( "mail.smtp.auth ", "true "); 
        Authenticator auth = new Authenticator();      // Get session 
        Session session = Session.getDefaultInstance(props, null);      // Define message 
        MimeMessage message = new MimeMessage(session);      // Set the from address 
        message.setFrom(new InternetAddress(from));      // Set the to address 
        message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to));      // Set the subject 
        message.setSubject("Hello JavaMail");      // Set the content 
        String mailBody = "Welcome to JavaMail";
        BodyPart bodyPart = new MimeBodyPart();
    bodyPart.setContent(mailBody, "text/html;charset=utf-8");
    multipart.addBodyPart(bodyPart);
        //message.setText("Welcome to JavaMail"); 

    // Set file
    String filename = "d:/aa/text.excel";
    BodyPart bodyPart1 = new MimeBodyPart();
    FileDataSource fileDataSource = new FileDataSource(
    filename);
    bodyPart1
    .setDataHandler(new DataHandler(fileDataSource));
    bodyPart1.setFileName(fileDataSource.getName());
    multipart.addBodyPart(bodyPart1);

    message.setContent(multipart); message.saveChanges();


        // Send message 
        Transport.send(message); 
        
        System.out.println("successfully send mail"); 
        
    }catch(Exception e){ 
    e.printStackTrace(); 
    System.out.println(e.getMessage()); 


    public static void main(String arg[]){ 
    MailSendBean mailSendBean=new MailSendBean(); 
    mailSendBean.sendMail(); 

    private class Authenticator extends javax.mail.Authenticator { 
    public PasswordAuthentication getPasswordAuthentication() 

    String un = "bxxyzj"; 
    String pw = "851004"; 
    return new PasswordAuthentication(un, pw); 
    }  } 

      

  4.   

    楼上的大哥,报错,空指针  路径我那样写有问题吗?
    used struction fMailSend
    enter sendMail method
    null
    java.lang.NullPointerException
    at com.eastcom_sw.cis.webapp.action.basedata.MailSendBean.sendMail(MailSendBean.java:64)
    at com.eastcom_sw.cis.webapp.action.basedata.MailSendBean.main(MailSendBean.java:94)
      

  5.   


    // Define message 
            MimeMessage message = new MimeMessage(session); 后面加入
    multipart = new MimeMultipart();
      

  6.   

    用;号 给你个方法
    private InternetAddress[] translateAddress(String to)
    throws AddressException {
    StringTokenizer tokens = new StringTokenizer(to, ";"); InternetAddress[] address = new InternetAddress[tokens.countTokens()];
    for (int i = 0; tokens.hasMoreTokens(); i++) {
    address[i] = new InternetAddress(tokens.nextToken());
    }
    return address;
    }把你要发送多个用户的email用;连接起来放在string to里
    message.setRecipients(Message.RecipientType.TO,
    translateAddress(to));
      

  7.   

    楼上的,问题奇怪了
    main方法测试通过,但我tomact起来后,直接调用sendMail()这个方法,确报错了
    为什么main方法测试可以通过,但tomact起来后调用这个方法,确总是报Could not connect to SMTP hostjavax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1)
    at javax.mail.Transport.send(Transport.java:163)
    at javax.mail.Transport.send(Transport.java:48)
    at com.eastcom_sw.cis.webapp.action.integrate.gis.GisFaultReportBean.sendMail(GisFaultReportBean.java:185)
    at com.eastcom_sw.cis.webapp.action.integrate.gis.GisFaultReportBean.saveGisReport(GisFaultReportBean.java:510)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:151)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
    at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
    at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
    at javax.faces.component.UICommand.broadcast(UICommand.java:106)
    at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
    at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
    at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
    at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:343)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:307)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at com.eastcom_sw.cis.webapp.filter.MessageFilter.doFilter(MessageFilter.java:45)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
    at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at net.sf.ehcache.constructs.web.filter.GzipFilter.doFilter(GzipFilter.java:75)
    at net.sf.ehcache.constructs.web.filter.Filter.doFilter(Filter.java:92)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at com.eastcom_sw.cis.webapp.filter.LocaleFilter.doFilterInternal(LocaleFilter.java:74)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
    at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
    at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:166)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternalCaused by: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1270)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:231)
    at javax.mail.Service.connect(Service.java:85)
    at javax.mail.Service.connect(Service.java:70)
    at javax.mail.Transport.send(Transport.java:94)
    ... 89 more
      

  8.   

    现在tomact启动后,调用sendMail()方法报错,main方法里测试确是正确的。这个就奇怪了啊,错误:
    Setting file name to text.xls
    javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1)
    at javax.mail.Transport.send(Transport.java:163)
    at javax.mail.Transport.send(Transport.java:48)
    at com.eastcom_sw.cis.webapp.action.integrate.gis.GisFaultReportBean.sendMail(GisFaultReportBean.java:185)
    at com.eastcom_sw.cis.webapp.action.integrate.gis.GisFaultReportBean.saveGisReport(GisFaultReportBean.java:510)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:151)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
    at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
    at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
    at javax.faces.component.UICommand.broadcast(UICommand.java:106)
    at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
    at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
    at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
    at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:343)
    Caused by: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1270)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:231)
    at javax.mail.Service.connect(Service.java:85)
    at javax.mail.Service.connect(Service.java:70)
    at javax.mail.Transport.send(Transport.java:94)
    ... 89 more
    Send failure (javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1)
      

  9.   

    >_<
    要求加分...
    问题一个接一个...
      

  10.   

    host: localhost, port: 25
    C:\WINDOWS\system32\drivers\etc\hosts 看有没有 127.0.0.1       localhost
    然后看25端口...
    再不行的话那就发送邮件的时候
    加用户名密码 邮件安全认证
      

  11.   

    大哥你qq或者msn多少,我加你问。分全是你的
      

  12.   

    说简单了,你的也就是多用户有附件的邮件发送。
    看看下边的代码吧。import java.util.Properties;
    import common.util.Email_Autherticatorbean;
    import javax.mail.Authenticator;
    import javax.mail.internet.InternetAddress;
    import org.apache.commons.lang.StringUtils;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.Multipart;
    import javax.activation.FileDataSource;
    import javax.mail.internet.MimeMultipart;
    import javax.activation.DataHandler;
    import javax.mail.internet.MimeUtility;
    import java.util.Date;/**
     * 利用java.mail的邮件发送程序
     */public class SendMailTest
    {
      public static void main(String[] args)
      {
        String title = "titleTest";//所发送邮件的标题
        String from ="[email protected]";//从那里发送
        String sendTo[] = {"[email protected]","[email protected]"};//发送到那里
        //邮件的文本内容,可以包含html标记则显示为html页面
        String content = "mail test!!!!!!<br><a href=#>aaa</a>";
        //所包含的附件,及附件的重新命名
        String fileNames[] = {"F://music//text1.txt,text1.txt","F://music//text2.txt,text2.txt"};
        try {
        //  MailSender mailsender = new MailSender();
          sendmail(title, from, sendTo, content, fileNames,"text/html;charset=gb2312");
          } catch (Exception ex) { ex.printStackTrace(); }
      }  public static void sendmail(String subject, String from, String[] to, String text, String[] filenames,String mimeType) throws Exception
      {
        //ResourceBundle mailProps = ResourceBundle.getBundle("mail");   可以从配置文件读取相应的参数
        Properties props = new Properties();    String smtp = "smtp.163.com";  //设置发送邮件所用到的smtp
        String servername = "sir_znp";
        String serverpaswd = "123";    javax.mail.Session mailSession; //邮件会话对象
        javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象    props = java.lang.System.getProperties(); //获得系统属性对象
        props.put("mail.smtp.host", smtp); //设置SMTP主机
        props.put("mail.smtp.auth", "true"); //是否到服务器用户名和密码验证
        //到服务器验证发送的用户名和密码是否正确
        Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(servername, serverpaswd);
        //设置邮件会话
        mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther); 
        //设置传输协议
        javax.mail.Transport transport = mailSession.getTransport("smtp");
        //设置from、to等信息
        mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
        if (!StringUtils.isEmpty(from))
           {
             InternetAddress sentFrom = new InternetAddress(from);
             mimeMsg.setFrom(sentFrom);  //设置发送人地址
           }    InternetAddress[] sendTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++)
           {
             System.out.println("发送到:" + to[i]);
             sendTo[i] = new InternetAddress(to[i]);
           }    mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, sendTo);
        mimeMsg.setSubject(subject, "gb2312");    MimeBodyPart messageBodyPart1 = new MimeBodyPart();
        //messageBodyPart.setText(UnicodeToChinese(text));
        messageBodyPart1.setContent(text, mimeType);    Multipart multipart = new MimeMultipart();//附件传输格式
        multipart.addBodyPart(messageBodyPart1);    for (int i = 0; i < filenames.length; i++) {
          MimeBodyPart messageBodyPart2 = new MimeBodyPart();
          //选择出每一个附件名
          String filename = filenames[i].split(",")[0];
          System.out.println("附件名:" + filename);
          String displayname = filenames[i].split(",")[1];
          //得到数据源
          FileDataSource fds = new FileDataSource(filename);
          //得到附件本身并至入BodyPart
          messageBodyPart2.setDataHandler(new DataHandler(fds));
          //得到文件名同样至入BodyPart
          //messageBodyPart2.setFileName(displayname);
          // messageBodyPart2.setFileName(fds.getName());
          messageBodyPart2.setFileName(MimeUtility.encodeText(displayname));
          multipart.addBodyPart(messageBodyPart2);
        }
        mimeMsg.setContent(multipart);
        //设置信件头的发送日期
        mimeMsg.setSentDate(new Date());
        mimeMsg.saveChanges();
        //发送邮件
        transport.send(mimeMsg);
        transport.close();
      }}验证类
    package common.util;
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    public class Email_Autherticatorbean  extends Authenticator
    {
    private String m_username = null;
    private String m_userpass = null;public void setUsername(String username)
    {
     m_username = username;
    }public void setUserpass(String userpass)
    {
     m_userpass = userpass;
    }public Email_Autherticatorbean(String username, String userpass)
    {
       super();
       setUsername(username);
       setUserpass(userpass);}
    public PasswordAuthentication getPasswordAuthentication()
    {  return new PasswordAuthentication(m_username,m_userpass);
    }
    }注释还可以,看不懂了再说吧。
      

  13.   

    楼上的我的问题是在TOMACT启动后调用报错,MAIN方法测试是通过的
      

  14.   

    mousetsun  我MAIN方法测试的时候接收邮件看不到发件人
    TOMACT下也存在这个 Could not connect to SMTP host: localhost, port: 25, response: -1的问题。