0

JSPを使用して、ファイルを送信せずにメールに添付したいボタンをクリックすると、JSPページが1つありますか?

そのボタンをクリックしてmailtoタグを使用しているため、gmailにログインでき、メールで件名を渡すことができますが、jspまたはJavaを使用してファイルを添付する方法がわかりませんか?

誰かがそれを行う方法を知っている場合は、教えてください。

4

3 に答える 3

0

いいえ、mailto を使用してメッセージに添付ファイルを追加することはできません。

mailto: ヘッダー値またはテキスト/プレーン コンテンツのみをサポートします。

于 2013-05-27T09:35:16.177 に答える
0
    //Mail.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@include file="Database.jsp" %>
    <%@page import="java.sql.Statement"%>
    <%@page import="java.sql.ResultSet"%>
    <html>
        <head>
        </head>
    <body>  
     <form id="form1" class="blocks" action="AttachMail.jsp" method="post" enctype="multipart/form-data">
           <table width="500px"  border="0">
            <tr>
                <td style="color:#BAA111" align="center"><h4><b>File Upload</b></h4></td>
            </tr>
            <tr>
                <td style="width:200px;">
                    <hr style="width:410px" align="left"></hr>
                </td>
            </tr>
        </table><br>
                <p>
                    <label>First Name:</label>
                    <input type="text" name="fname" class="text"/>
                </p>
                <p>
                    <label>Last Name:</label>
                    <input type="text" name="lname" class="text"/>
                </p>
                <p>
                    <label>Email ID:</label>
                    <input type="text" name="email" class="text"/>
                </p>
                <p>         
                    <label>Upload File:</label>             
                    <input type="file" name="file" class="file"/>           
                </p>
                <p><br>
                    <label>&nbsp;&nbsp;&nbsp;</label>
                    <input type="submit" class="btn" value="Submit"/>
                    <label style="width:10px"></label>
                    <input type="reset" class="btn" value="Reset"/>
                </p>          
         </form>  
    </body>
    </html>


    //AttachMail.jsp

    <%@ page import="java.util.*,java.io.*,javax.mail.*,javax.mail.internet.*,javax.activation.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.servlet.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.util.*" %>
    <html>
    <body>
        <%
        String subject="Sending Mail with Attachment and Form data";//This is fixed subject, you may get from "form" also
        String body="";

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iter = upload.getItemIterator(request);
            FileItemStream item = null;
            String fname = null; //you may write different names also
            String lname = null; // or put in default value  
            String email = null; //  for these fields
            String file = null;
            InputStream stream = null;
            //out.print("mulipart request received<br/>");
            while (iter.hasNext()) {
                item = iter.next();
                String fdname = item.getFieldName();
                stream = item.openStream();
                if (item.isFormField()) {
                    String fieldValue = Streams.asString(stream);
                    //out.print("Form field " + name + " with value "   + fieldValue + "<br/>");
                    if ("fname".equals(fdname)){
                         body ="First Name : "+ fieldValue+"\n";
                    }
                    if ("lname".equals(fdname)){
                        body =body+"Last Name : "+ fieldValue+"\n";
                    }
                    if ("email".equals(fdname)){
                        email=fieldValue;
                        body =body+"Email Id : "+ fieldValue+"\n";
                    }
                }

                else {
                    fdname = item.getName();
                    if (fdname != null && !"".equals(fdname.trim())) 
                    {
                        file = new File(item.getName()).getName();

                        FileDataSource fds = new FileDataSource(file);
                    try{
                        Properties props = new Properties(); //we could enter properties later
                        props.put("mail.smtp.auth", "true");
                        Session s = Session.getInstance(props, null);
                        MimeMessage msg = new MimeMessage(s);
                        msg.setFrom(new InternetAddress(email));
                        msg.addRecipient(Message.RecipientType.TO,new InternetAddress("To mail address"));//or you can pass from form also
                        msg.setSubject(subject);
                        MimeBodyPart mbp1 = new MimeBodyPart();
                        mbp1.setText(body);
                        MimeBodyPart mbp2 = new MimeBodyPart();
                        mbp2.setDataHandler(new DataHandler(fds));
                        mbp2.setFileName(fds.getName());
                        Multipart mp = new MimeMultipart();
                        mp.addBodyPart(mbp1);
                        mp.addBodyPart(mbp2);
                        msg.setContent(mp);
                        msg.saveChanges();
                        Transport transport=s.getTransport("smtp");
                        transport.connect("smtp domain name","email address","password");
                        transport.sendMessage(msg,msg.getAllRecipients());
                        transport.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                   } 
                    else
                        out.print("File or necessary fields were not sent !<br/>");
                }
            }
        } else
            out.print("request was not multipart");
    %>
    </body>
    </html> 

//jar files to add "mail.jar, naming-factory-4.1.31.jar, org.apache.commons.fileuplod.jar"
于 2013-06-01T16:09:24.057 に答える