如何回复包含附件的邮件?

时间:2011-12-01 11:13:00

标签: javamail

我创建了一个显示邮件的电子邮件客户端,当我点击回复时,应该使用附件回复邮件,但我的enctype='multipart/form-data'>并未接受bodypart.getContent()或任何其他方法。 请尽快帮助我解决这个问题,因为我必须接受我的项目。

composereplymail.jsp

<%@ page import="user.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.io.*" %>
<%
    String sendmessage=(String)request.getAttribute("sendmessage");
    if(sendmessage==null)
    {
        sendmessage="";
    }

    if (session == null) {
        response.sendError(404, "Session expired");
        return;
    }
    userinfo info = (userinfo)session.getAttribute("userinfo");

    Session sessio = info.getSession();
    Store store = sessio.getStore("imap");
      store.connect(info.getHostname(),info.getusername(),info.getpassword());
    Folder folder = store.getFolder(info.getUrl()); 
    folder.open(Folder.READ_WRITE);
    Integer messageno=Integer.parseInt(request.getParameter("messageno"));
    Message[] message = folder.getMessages();
    String subject=message[messageno].getSubject();


%>



<html><head>
<title>Home Page</title>
<link rel="stylesheet" type="text/css" href="styleforcomposemail.css" />
</head>
  <body>
<center>
<form action='sendmail' method='post' enctype='multipart/form-data'>
    <table>
       <tr bgcolor='#800517'>
        <td colspan='2'><b><h3><center>Compose Your Mail Here</center></h3></b></td>
       </tr>
       <tr height="15" bgcolor='#CFECEC'>
        <td><b>From :</td>
        <td><label for="login"><%= info.getsendfrom() %></label></td>
       </tr>
       <tr height="15" bgcolor='#CFECEC'>
        <td><b>TO :</td>
        <td><input type='text' name='sendto' class='Large' value='<%= message[messageno].getFrom()[0]  %>' /></b>separate addresses with commas</td>
       </tr>
       <tr height="15" bgcolor='#CFECEC'>
        <td><b  >CC :</td>
        <td><input type='text' name='cc' class='Large' /></b></td>
       </tr>
       <tr height="15" bgcolor='#CFECEC'>
        <td><b>Subject :</td>
        <td><input type='text' name='subject' class='Large' value='Re : <%=subject%>' /></td>
       </tr>


<%

MimeMessage reply = (MimeMessage) message[messageno].reply(false);

Multipart multipart=(Multipart)message[messageno].getContent();

for(int i=0;i<multipart.getCount();i++)
{
    BodyPart bodypart = multipart.getBodyPart(i);

    String disposition = bodypart.getDisposition();

    if(disposition != null && (disposition.equals(BodyPart.ATTACHMENT)))
    {

        //saveFile(bodypart.getFileName(),bodypart.getInputStream());


    String filename=bodypart.getFileName();

    String FilePath="C:\\nikki\\apache-tomcat-7.0.22\\webapps\\Email\\download\\"+filename;
    String FileDownloadPath="\\Email\\download\\"+filename;
    DataHandler dh = bodypart.getDataHandler();
    %>

       <tr height="15" bgcolor='#CFECEC'>

        <td><b>Attachment :</td>
        <td><input type="hidden" name="attachment" value="<%=bodypart.getContent()%>"/><a href="<%=FileDownloadPath%>"><%=filename%></a></td>//**what method should i pass over here to get that file sent with multipart/form-data**
       </tr>
    <%}else{%>

       <tr bgcolor='#CFECEC'>
        <td colspan='2'><textarea id="textAreaa" rows='32' cols='135' name='data'><%=bodypart.getContent()%></textarea></td>
       </tr>
    <%}}%>
       <tr height="15" bgcolor='#CFECEC'>
        <td colspan='2'><input type='submit' class='button' name='Submit' value='Submit'/>
                <input type='submit' class='button' name='Draft' value='Save As Draft'/>
                <input type='reset' class='button' value='Reset' name='Reset'/>
                <input type='reset' class='button' value='Discard' name='Reset'/>
        </td>
       </tr>
    </table>
    </form>
  </center>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

我并不完全清楚你要做什么......

通常,在回复邮件时,不要希望回复包含原始邮件的所有附件。

无论如何,您绝对不希望仅将附件发送到浏览器客户端,以便它可以作为附件发送给您。也许您真正想要的是允许用户选择将附加到回复的文件?

JavaMail下载包包括几个演示程序,一个基于servlet,一个基于JSP,你看过它们吗?