我在我的jsp中有一个文本框,并希望发送一封电子邮件给他/她在电子邮箱中输入的电子邮件。
请你指导一下如何做到这一点。
我刚刚查看了这段代码:
<html>
<head>
<title>mailto Example</title>
</head>
<body>
<form action="mailto:XXX@XXX.com" 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>
答案 0 :(得分:0)
执行此操作的常见方法是使用某些服务器端脚本,例如:在php中,它将采用表单的值,从中创建一封电子邮件并发送。
表单数据当然可以通过javascript / ajax发送,但我认为在使用php脚本时不需要。
答案 1 :(得分:0)
您需要将表单发布到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教程,请检查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);
}