【问题标题】:create contact form in jsp?在jsp中创建联系表格?
【发布时间】:2016-02-01 22:58:42
【问题描述】:

我可以使用以下代码和 Javamail API 发送电子邮件,通过它我可以使用“FROM”作为我的“雅虎电子邮件 ID”发送到任何电子邮件 ID。代码:

mailFORM.html

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Mail form in html</title>
    </head>
    <body>
    <table border="1" width="50%"  cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%">
    <form method="POST" action="mail.jsp">
    <table border="1" width="100%" cellpadding="0" cellspacing="0">
    <h1>Mail API</h1>
    <tr>
    <td width="50%"><b>To:</b></td>
    <td width="50%"><input type="text" name="to" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>From:</b></td>
    <td width="50%"><input type="text" name="from" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>Subject:</b></td>
    <td width="50%"><input type="text" name="subject" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>Description:</b></td>
    <td width="50%"><textarea name="description" type="text" 
cols="40" rows="15" size=100>
    </textarea>
    </td>
    </tr>
    <tr>
    <td><p><input type="submit" value="Send Mail" name="sendMail"></td>
    </tr>
    </table>
    </p>
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>  

mail.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
    javax.mail.internet.*,com.sun.mail.smtp.*"%>
    <html>
    <head>
    <title>sending mail using contactus form</title>
    </head>
    <body>
    <%
    try{
    Session mailSession = Session.getInstance(System.getProperties());
    Transport transport = new SMTPTransport(mailSession,new URLName("smtp.mail.yahoo.com"));
    transport = mailSession.getTransport("smtps");
    transport.connect("smtp.mail.yahoo.com",465,"myyahooid@yahoo.com","myyahoopassword");
   MimeMessage m = new MimeMessage(mailSession);
   m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
   Address[] toAddr = new InternetAddress[] {
   new InternetAddress(%><%request.getParameter("to")%><%)
   };
   m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
   m.setSubject(%><%request.getParameter("subject")%><%);
   m.setSentDate(new java.util.Date());
   m.setContent(%><%request.getParameter("description")%><%, "text/plain");

   transport.sendMessage(m,m.getAllRecipients());
   transport.close();
   out.println("Thanks for sending mail!"); 
   }
   catch(Exception e){
   out.println(e.getMessage());
   e.printStackTrace();
   }
   %>
   </body>  

现在,我想创建一个简单的 CONTACTUS 表单,出于显而易见的原因,我将从“mailFORM.html”文件中删除“TO”字段。因为我只希望任何访问者访问我的网站并通过输入 FROM、NAME、SUBJECT 和描述向“mycompanyid@domain.com”发送电子邮件。

那么我该如何消除输入用户名和密码的问题。因为我无法创建在这里输入密码的代码。我无法为不同的 smtp 创建单独的代码...因为访问我的网站联系页面的 VISITIR 可以通过输入来自任何域(例如 gmail、yahoo 等)的电子邮件来填写表格。

总之,我只想创建这样的表单(使用任何匿名网站),它将详细信息发送到公司的特定电子邮件 ID,例如“mycompanyid@domain.com”
http://www.tutorialspoint.com/about/contact_us.htm

【问题讨论】:

    标签: java html jsp jakarta-mail


    【解决方案1】:

    在您的 JSP 文件中硬编码您的电子邮件 ID,但最好避免在 JSP 中编写您的 java 代码,应该只编写方法调用。

    private final String TO_EMAIL = "something@domain.com";
    

    查看我的代码,它适用于所有 SMTPS。

    package com.naveed.workingfiles;
    
    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.EmailAttachment;
     import org.apache.commons.mail.MultiPartEmail;
    
    public class Mail {
    String senderID;
    String senderPassword;
    String hostName;
    int portNumber;
    String attachmentPath;
    String subject;
    String body;
    String cc;
    String bcc;
    
    
    // #=============================================================================================#
    public String getBcc() {
        return bcc;
    }
    
    // #=============================================================================================#
    public void setBcc(String bcc) {
        this.bcc = bcc;
    }
    
    // #=============================================================================================#
    public String getCc() {
        return cc;
    }
    
    // #=============================================================================================#
    public void setCc(String cc) {
        this.cc = cc;
    }
    
    // #=============================================================================================#
    public String getBody() {
        return body;
    }
    
    // #=============================================================================================#
    public void setBody(String body) {
        this.body = body;
    }
    
    // #=============================================================================================#
    public String getSubject() {
        return subject;
    }
    
    // #=============================================================================================#
    public void setSubject(String subject) {
        this.subject = subject;
    }
    
    // #=============================================================================================#
    
    public String getSenderID() {
        return senderID;
    }
    
    // #=============================================================================================#
    public void setSenderID(String senderID) {
        this.senderID = senderID;
    }
    
    public String getSenderPassword() {
        return senderPassword;
    }
    
    // #=============================================================================================#
    public void setSenderPassword(String senderPassword) {
        this.senderPassword = senderPassword;
    }
    
    // #=============================================================================================#
    public String getHostName() {
        return hostName;
    }
    
    // #=============================================================================================#
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    
    // #=============================================================================================#
    public int getPortNumber() {
        return portNumber;
    }
    
    // #=============================================================================================#
    public void setPortNumber(int portNumber) {
        this.portNumber = portNumber;
    }
    
    // #=============================================================================================#
    public String getAttachmentPath() {
        return attachmentPath;
    }
    
    // #=============================================================================================#
    public void setAttachmentPath(String attachmentPath) {
        this.attachmentPath = attachmentPath;
    }
    
    // #=============================================================================================#
    public void sendMail(String receiverId) {
    
        try {
            // this below commented line for the HTML body text
            // MultiPartEmail htmlEmail = new HtmlEmail();
            // OR
            // HtmlEmail email = new HtmlEmail();
    
            MultiPartEmail email = new MultiPartEmail();
            // setting the port number
            email.setSmtpPort(getPortNumber());
            // authenticating the user
            email.setAuthenticator(new DefaultAuthenticator(getSenderID(),
                    getSenderPassword()));
            // email.setDebug(true);
            email.setSSL(true);
            // setting the host name
            email.setHostName(getHostName());
            // setting the rciever id
    
            email.addTo(receiverId);
    
            // check for user enterd cc or not
            if (getCc() != null) {
                // add the cc
                email.addCc(getCc());
            }
            // check for user enterd bcc or not
            if (getBcc() != null) {
                // add the bcc
                email.addBcc(getBcc());
            }
            // setting the sender id
            email.setFrom(getSenderID());
            // setting the subject of mail
            email.setSubject(getSubject());
            // setting message body
            email.setMsg(getBody());
            // email.setHtmlMsg("<h1>"+getBody()+"</h1>");
    
            // checking for attachment attachment
            if (getAttachmentPath() != null) {
                // add the attachment
                EmailAttachment attachment = new EmailAttachment();
                attachment.setPath(getAttachmentPath());
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                email.attach(attachment);
            }
    
            // send the email
            email.send();
            // System.out.println("Mail sent!");
        } catch (Exception e) {
            // System.out.println("Exception :: " + e);
            e.printStackTrace();
    
        }
    }// sendmail()
    }// mail
    

    仅您需要保存所有相应的域详细信息(主机名、端口),基于用户电子邮件设置所有设置器。 下载所有需要的库。

    【讨论】:

    • 好的@NAVEEED,那么我提到的其他事情呢......通过避免为服务器输入用户名/密码来在jsp中创建联系人表单。先生,请再看一遍帖子
    • @nAvEed...我阅读了您的代码(但使用了我的代码),它很棒,可以帮助其他人。因此,出于同样的原因,我接受它作为答案。 :)
    【解决方案2】:

    您可以硬核Message.RecipientType.TO 变量。

    您可以在某处定义静态变量MY_MAIL 并使用MY_MAIL 而不是request.getParameter("to")

    请确保在代码中的某处包含发件人的电子邮件,以便您可以在必要时与他们联系。

    还有其他一些注意事项:

    • 请阅读有关从 JSP 页面中分离实际 Java 代码的信息,您可以在 google 上找到足够的链接。
    • 您正在导入类,但不再使用导入。
    • 代码中有无用的%&gt;&lt;%,我什至不知道它们在哪里。

    【讨论】:

    • 好的,考虑到你提到的......我知道在某处存储 TO 地址。直到我想解决其他规格。请回答我的要求。我会尽量遵循您的考虑
    • @AmanChawla 这是不可能的。您的代码显示了如何将电子邮件从已知地址(使用用户名/密码登录)发送到某个目的地。您永远不会真正以您的用户身份登录。所以基本上考虑给自己发邮件。例如从网络服务器到网络服务器,或者从网络服务器到您的个人邮件等。
    • 好的@skiwi,知道我该怎么做..这个网站上的许多用户和其他用户通过输入用户名和密码以同样的方式进行操作。所以,我想知道访问者如何在 jsp 中创建联系人/反馈表单,只需输入他/她的电子邮件 ID、主题和消息即可发送电子邮件。
    • @AmanChawla 好吧,我的意思是这样做本身并没有错,但它需要您构建所有可能域的表,然后让用户信任您来处理他的凭据小心。如果您想摆脱用户/密码,那么您不能让用户通过他自己的帐户直接发送邮件给您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多