【问题标题】:On submit the information should come to email [closed]提交信息应通过电子邮件发送[关闭]
【发布时间】:2015-09-10 10:34:24
【问题描述】:

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

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

  1. 姓名
  2. 电子邮件
  3. 号码
  4. 详情

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

【问题讨论】:

  • 我不明白为什么人们不在问题中添加代码
  • 您要我为您工作吗?但你必须付出同样的代价。 :)
  • 什么是联系我们页面?

标签: javascript


【解决方案1】:

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);

    }
}

请跟随 cmets 了解代码流程。

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 2012-04-11
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2018-12-16
    • 2016-05-16
    相关资源
    最近更新 更多