发送电子邮件到文本框中的文本“电子邮件”
我在我的jsp中有一个文本框,并且想要发送一封电子邮件到他/她的电子邮件进入的receipent在文本框中。发送电子邮件到文本框中的文本“电子邮件”
请你指导我如何做到这一点。
我刚刚签出此代码:
<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>
您需要将表单发布到servlet,并从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的教程检查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);
}
我可以在同一个jsp中做到这一点吗?只是为了测试 – maas 2010-09-15 10:16:20
你可以,我不会推荐它,最好把它放在Servlet上, – 2010-09-15 10:30:26
常见的方式做到这一点是有一些服务器端脚本,例如在PHP中,这将采取形式的价值观,创建一个电子邮件并发送它。
表单数据当然可以通过javascript/ajax发送,但我认为在使用php脚本时不需要。
要打开一个本地用户系统上的电子邮件客户端还是要从服务器发送电子邮件? – 2010-09-15 10:04:05