Closed. This question needs details or clarity。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                
                    4年前关闭。
            
        

    

我的网页上有一个“查询”标签。

查询选项卡包含以下信息:


名称
电子邮件

细节


单击提交按钮后,上述详细信息应直接发送到电子邮件地址。我已经在“联系我们”页面中提供了我的电子邮件地址。
但是我对如何将查询中提到的信息发送到我的电子邮件地址一无所知。

最佳答案

JavaMail api会有所帮助。从http://www.oracle.com/technetwork/java/index-138643.html下载

//根据您的要求更改代码。这项工作对我来说很完美。

index.html

<form action="EmailSendingServlet" method="post">
  To:<input type="text" name="recipient"  /><br/>
  Subject:<input type="text" name="subject"/><br/>
  Message:<input type="text" name="content"/><br/>
  <input type="submit" value="Send Mail" />
</form>


web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">


    <!-- SMTP settings -->
    <context-param>
        <param-name>host</param-name>
        <param-value>smtp.gmail.com</param-value>
    </context-param>

    <context-param>
        <param-name>port</param-name>
        <param-value>587</param-value>
    </context-param>

    <context-param>
        <param-name>user</param-name>
        <param-value>yourmailID</param-value>
    </context-param>

    <context-param>
        <param-name>pass</param-name>
        <param-value>yourpassword</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>


确保将yourmailID更改为有效的发件人邮件ID,并将yourpassword更改为正确的密码。

由于我的文件位于文件夹notify / email /中,因此我已包含了该软件包。对您的代码进行适当的更改。

EmailSendingServlet.java

package notify.email;
import java.util.*;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.fastinfoset.sax.Properties;
@WebServlet("/EmailSendingServlet")
public class EmailSendingServlet extends HttpServlet {
    private String host;
    private String port;
    private String user;
    private String pass;
    //private String proxyHost;
    //private String proxyPort;

    public void init() {
        // reads SMTP server setting from web.xml file
        ServletContext context = getServletContext();
        host = context.getInitParameter("host");
        port = context.getInitParameter("port");
        user = context.getInitParameter("user");
        pass = context.getInitParameter("pass");
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // reads form fields
        String recipient = request.getParameter("recipient");
        String subject = request.getParameter("subject");
        String content = request.getParameter("content");
        /*proxyHost="127.0.0.1";
        proxyPort="8080";
        java.util.Properties proxySet = System.getProperties();
        proxySet.put("http.proxyPort", proxyPort);
        proxySet.put("http.proxyHost", proxyHost);*/
        String resultMessage = "";

        try {
            EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
                    content);
            resultMessage = "The e-mail was sent successfully";
        } catch (Exception ex) {
            ex.printStackTrace();
            resultMessage = "There were an error: " + ex;
        }
    }
}


注释的行用于通过代理服务器发送邮件。

EmailUtility.java

package notify.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtility {
    public static void sendEmail(String host, String port,
            final String userName, final String password, String toAddress,
            String subject, String message) throws AddressException,
            MessagingException {

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };

    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    // sends the e-mail
    Transport.send(msg);

    }
}


请按照注释了解代码流。

关于javascript - 提交信息后,请发送电子邮件至,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31022913/

10-16 23:22