weixin_42298075
2008-11-17 13:28
浏览 272
已采纳

javaMail 邮件发送

javaMail中发送附件时附件
1. 从一个文件中获得(FileDataSource)
2. 从一个URL处获得(URLDataSource)
现在需要发送一个pdf格式的附件(用iText生成的),目前的做法是先将生成的pdf附件放到服务器的一个临时文件夹中,等发送以后再将服
务器上的这个pdf文件删除。

请问: 能否直接将生成pdf文件的输出流(通过response.getOutputStream()得到)当作"附件"发送,而不是将它生成一个pdf文件再发送?
(最主要的是不想将它保存到服务器上发送完了再删除)
[b]问题补充:[/b]
javaMail中发送附件时附件
1. 从一个文件中获得(FileDataSource)
2. 从一个URL处获得(URLDataSource)
现在需要发送一个pdf格式的附件(用iText生成的),目前的做法是先将生成的pdf附件放到服务器的一个临时文件夹中,等发送以后再将服
务器上的这个pdf文件删除。

请问: 能否直接将生成pdf文件的输出流(通过response.getOutputStream()得到)当作"附件"发送,而不是将它生成一个pdf文件再发送?
(最主要的是不想将它保存到服务器上发送完了再删除)

问题解决:
public class javax.mail.util.ByteArrayDataSource implements javax.activation.DataSource{}

public ByteArrayDataSource(byte[] arg0, java.lang.String arg1)

还是谢langhua9527

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • langhua9527 2008-11-17 13:54
    已采纳

    强烈郁闷中,发部了不能编辑

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • langhua9527 2008-11-17 13:46

    我给你个例子吧..
    [code="java"]
    <%@ page contentType="text/html; charset=utf-8" language="java" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



    SendMail <!-- .STYLE1 { color: #FF0000; font-size: 18px; } -->

    <br> window.onload=function(){<br> var editor = new FCKeditor(&#39;content&#39;);<br> editor.BasePath =&#39;/fileupload/fckeditor/&#39;;<br> editor.Height = 200;<br> editor.ToolbarSet=&#39;Basic&#39;;<br> editor.ReplaceTextarea();<br> }<br>

    邮件发送系统 1.2



    收件人





    发件人





    标题





    附件





    内容













     
     




    [/code]

    评论
    解决 无用
    打赏 举报
  • langhua9527 2008-11-17 13:48

    servlet/uploadandemail
    [code="java"]
    package com.langhua;

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.MultiPartEmail;

    public class UploadandEmailServlet extends HttpServlet {

    private static final long serialVersionUID = 7930014383778955337L;
    private String starPath;
    private ServletContext sc;
    private Map<String,String> params = new HashMap<String,String>();
    File tempFile = null;
    
    public void init(ServletConfig config){
        starPath = config.getInitParameter("savePath");
        sc = config.getServletContext();
    }
    
    public File doAttachment(HttpServletRequest request){
        File file = null;
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List items = upload.parseRequest(request);
            Iterator it = items.iterator();
            while(it.hasNext()){
                FileItem item = (FileItem) it.next();
                if(item.isFormField()){
                    params.put(item.getFieldName(),item.getString("utf-8"));
                }else{
                    if(item!=null&&!item.getName().equals("")){
                        tempFile = new File(item.getName());
                        file = new File(sc.getRealPath("/")+starPath,tempFile.getName());
                        item.write(file);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        File file = this.doAttachment(request);
        MultiPartEmail email = new MultiPartEmail();
        email.setCharset("utf-8");
        email.setHostName("smtp.qq.com");
        email.setAuthentication("172027141","lovelychun");
        try {
            email.addTo(params.get("to"));
            email.setFrom(params.get("from"));          
            email.setSubject(params.get("title"));
            email.setMsg(params.get("content"));
            if(file!=null){
                EmailAttachment attachment = new EmailAttachment(); 
                attachment.setPath(file.getPath());
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setName(file.getName());
                email.attach(attachment);
            }
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
            request.setAttribute("fileload","邮件发送不成功");
        }
        request.setAttribute("fileload","邮件发送成功");
        request.getRequestDispatcher("/show.jsp").forward(request,response);
    
    }
    

    }

    [/code]

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题