发送电子邮件至文本“电子邮件”在文本框中

发布于 2024-09-19 03:08:11 字数 610 浏览 5 评论 0原文

我的 jsp 中有一个文本框,想向收件人发送一封电子邮件,他/她的电子邮件被输入到文本框中。

您能指导我如何做到这一点吗?

我刚刚检查了这段代码:

<html>
<head>
    <title>mailto Example</title>
</head>

<body>
<form action="mailto:[email protected]" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

I am having a textbox in my jsp and would like to send an e-mail to the receipent which his/her e-mail is entered in the textbox.

Can you please guide me on how to do that.

I have just checked out this code:

<html>
<head>
    <title>mailto Example</title>
</head>

<body>
<form action="mailto:[email protected]" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-09-26 03:08:11

您需要将表单发布到 servlet,并从 servlet 执行此方法来发送邮件。
你的表单应该是

<form action="sendMail.do" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>

下面是从java发送电子邮件的代码,在web.xml中为servlet进行正确的映射
有关 servlet 教程,请查看此处

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}

You need to post your form to a servlet and from servlet execute this method to send mail.
your form should be

<form action="sendMail.do" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>

Here is code to send email from java,do proper mapping for servlet in web.xml
For servlet tutorials check it here

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
昇り龍 2024-09-26 03:08:11

执行此操作的常见方法是使用一些服务器端脚本(例如在 php 中),它将获取表单的值,从中创建电子邮件并发送它。

表单数据当然可以通过 javascript / ajax 发送,但我认为在使用 php 脚本时没有必要。

The common way to do this is to have some server side script, e.g. in php, which will take the form's values, create an email from that and send it.

The form data could of course be sent via javascript / ajax, but that I think would not be necessary when using a php script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文