java - 通过电子邮件发送大附件

标签 java

是否有可用的软件包可以有效地发送带有大附件的电子邮件(每个文件的上限为 10mb,但可以包含多个文件)。如果没有,是否有关于适当设计的建议,该设计不会导致内存不足异常,从而导致部署在同一服务器上的应用程序出现问题?

文件通过ftp传送到应用服务器。传输完成后,将调用 Web 服务(事务的元数据)。根据业务规则,此服务可能需要通过电子邮件发送文件。

我最初的想法是将请求放在消息队列上(以便服务可以立即返回),并使用同步方法处理请求(因此同时或大约同时的多个请求不会炸毁堆) 。

<小时/>

使用代码更新

messageBodyPart = new MimeBodyPart();
FileDataSource fileDataSource =new FileDataSource("locationTo.big.file");
messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
messageBodyPart.setFileName("big.file");
multipart.addBodyPart(messageBodyPart);
<rinse..repeat>

message.setContent(multipart);

Transport.send(msg);

如果我附加 5 个 10mb 附件,50mb 不会一次被堆吃掉吗?

最佳答案

为什么不使用执行器,线程池在合理范围内增长/收缩。提交的每个任务都是 Runnable 或 Callable。任务通过 JavaMail 发送,如果您为附件和/或消息正文实现自己的数据源实现,则不会占用太多内存。 (我假设您可以通过 InputStream 访问附件)

添加代码作为示例(请注意,此代码是多年前编写的,由于多种原因,它非常糟糕。但它显示了概念)

public static void sendMailAndThrowException(SMTPParams sparams,String  subject, DataSource msgTextSource,DataSource[] fids,boolean debug) throws MessagingException {
    Session session=getMailSession(sparams);
PrintStream f = null;
if (debug) {            
        f= getPrintStream();        
} 
// null is System.out by javamail api
session.setDebug(debug);
session.setDebugOut(f);     

try
{
    // create a message
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(sparams.getFrom()));

    // Recipients are comma delimitted
    String to_list[] = sparams.getRecipients().split(",");
    InternetAddress[] address = new InternetAddress[to_list.length];
    for( int i=0; i< to_list.length; i++)
    {
        // MJB: remove extraneous spaces, sanity check
        String temp = to_list[i].trim();
        if (temp.length()>0) {
            address[i] = new InternetAddress(to_list[i].trim());
        }
    }
    // Addresses are always TO, never CC  or BCC in this library
    msg.setRecipients(Message.RecipientType.TO, address);
        if ((msg.getAllRecipients() == null) || (msg.getAllRecipients().length==0)) {
            throw new MessagingException("No valid recipients");
        }

    // Set the subject
    msg.setSubject(subject,"UTF-8");

    // create the Multipart and add its parts to it
    Multipart mp = new MimeMultipart();


    if (msgTextSource != null) {
        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setDataHandler(new DataHandler(msgTextSource));
        mp.addBodyPart(mbp1);
    }

    if( fids != null)
    {
        for (int i=0;i<fids.length;i++) {
            //  create the second message part
            if (fids[i]==null) continue;
            MimeBodyPart mbp2 = new MimeBodyPart();
            //  attach the file to the message
            mbp2.setDataHandler(new DataHandler(fids[i]));
            mbp2.setFileName(fids[i].getName());
            mp.addBodyPart(mbp2);
        }
    }

    // add the Multipart to the message
    msg.setContent(mp);

    // set the Date: header
    msg.setSentDate(new java.util.Date());

    // Connect to SMTP server
    smtpSend(session, msg, sparams);

}

catch (MessagingException mex)
{
    throw mex;
} finally {
    closeDebug(f);
}


}

关于java - 通过电子邮件发送大附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049119/

相关文章:

java - 根据给定序列对 vector 进行排序

java - 插件 View 图标无法正确显示

java - ((某事).this).something_else

java - 将 JSON 对象传递给 Rythm 模板

java - 使用右移运算符屏蔽符号扩展 >>

java - 如何使MySQL表的某一列不可见

java - 如何使用 wicket 和 javascript 打印 pdf 文件

java - 在不同的 eclipse 实例中调试 tomcat 中的两个应用程序

java - 我无法将这两种方法联系在一起

java - Hive 表为空检查