Java 在jsp中创建联系人表单?
我可以使用以下代码和Javamail API发送电子邮件,通过Javamail API,我可以使用“发件人”作为我的“雅虎电子邮件id”发送到任何电子邮件id。守则: mailFORM.htmlJava 在jsp中创建联系人表单?,java,html,jsp,jakarta-mail,Java,Html,Jsp,Jakarta Mail,我可以使用以下代码和Javamail API发送电子邮件,通过Javamail API,我可以使用“发件人”作为我的“雅虎电子邮件id”发送到任何电子邮件id。守则: mailFORM.html <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Mail form
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Mail form in html</title>
</head>
<body>
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<tr>
<td width="100%">
<form method="POST" action="mail.jsp">
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<h1>Mail API</h1>
<tr>
<td width="50%"><b>To:</b></td>
<td width="50%"><input type="text" name="to" size="30"></td>
</tr>
<tr>
<td width="50%"><b>From:</b></td>
<td width="50%"><input type="text" name="from" size="30"></td>
</tr>
<tr>
<td width="50%"><b>Subject:</b></td>
<td width="50%"><input type="text" name="subject" size="30"></td>
</tr>
<tr>
<td width="50%"><b>Description:</b></td>
<td width="50%"><textarea name="description" type="text"
cols="40" rows="15" size=100>
</textarea>
</td>
</tr>
<tr>
<td><p><input type="submit" value="Send Mail" name="sendMail"></td>
</tr>
</table>
</p>
</form>
</td>
</tr>
</table>
</body>
</html>
html格式的邮件表单
邮件API
致:
发件人:
主题:
说明:
mail.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
javax.mail.internet.*,com.sun.mail.smtp.*"%>
<html>
<head>
<title>sending mail using contactus form</title>
</head>
<body>
<%
try{
Session mailSession = Session.getInstance(System.getProperties());
Transport transport = new SMTPTransport(mailSession,new URLName("smtp.mail.yahoo.com"));
transport = mailSession.getTransport("smtps");
transport.connect("smtp.mail.yahoo.com",465,"myyahooid@yahoo.com","myyahoopassword");
MimeMessage m = new MimeMessage(mailSession);
m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
Address[] toAddr = new InternetAddress[] {
new InternetAddress(%><%request.getParameter("to")%><%)
};
m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
m.setSubject(%><%request.getParameter("subject")%><%);
m.setSentDate(new java.util.Date());
m.setContent(%><%request.getParameter("description")%><%, "text/plain");
transport.sendMessage(m,m.getAllRecipients());
transport.close();
out.println("Thanks for sending mail!");
}
catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
%>
</body>
使用contactus表单发送邮件
现在,我想创建一个简单的CONTACTUS表单,其中我将从“mailFORM.html”文件中删除“to”字段,原因很明显。因为我只希望任何访问者访问我的网站并发送电子邮件至“mycompanyid@domain.com“输入发件人、姓名、主题和描述
那么,如何才能消除输入用户名和密码的问题呢。因为我不能创建一个在这里输入passord的代码。我不能为不同的smtp创建单独的代码…因为访问我的网站contactus页面的访问者可以通过输入他/她的来自任何域(如gmail、yahoo等)的电子邮件来填写表格
最后,我只想创建这样的表单(使用任何匿名网站),将详细信息发送到公司的特定电子邮件ID,如“mycompanyid@domain.com“您只需将
Message.RecipientType.TO
变量硬核化即可
在某个地方,您可以定义一个静态变量myu-MAIL
,并使用myu-MAIL
代替request.getParameter(“to”)
但是,请确保在代码中的某个地方包含发件人的电子邮件,以便您可以在必要时与他们联系
还有一些其他考虑:
- 请阅读关于从JSP页面分离实际Java代码的内容,您可以在google上找到足够的链接
- 您正在导入类,但之后就再也不使用导入了
- 在JSP文件中有一些无用的
%>硬代码用于电子邮件id,但最好避免在JSP中编写java代码,只编写方法调用
请参阅我的代码,它适用于所有SMTPprivate final String TO_EMAIL = "something@domain.com";
package com.naveed.workingfiles; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.MultiPartEmail; public class Mail { String senderID; String senderPassword; String hostName; int portNumber; String attachmentPath; String subject; String body; String cc; String bcc; // #=============================================================================================# public String getBcc() { return bcc; } // #=============================================================================================# public void setBcc(String bcc) { this.bcc = bcc; } // #=============================================================================================# public String getCc() { return cc; } // #=============================================================================================# public void setCc(String cc) { this.cc = cc; } // #=============================================================================================# public String getBody() { return body; } // #=============================================================================================# public void setBody(String body) { this.body = body; } // #=============================================================================================# public String getSubject() { return subject; } // #=============================================================================================# public void setSubject(String subject) { this.subject = subject; } // #=============================================================================================# public String getSenderID() { return senderID; } // #=============================================================================================# public void setSenderID(String senderID) { this.senderID = senderID; } public String getSenderPassword() { return senderPassword; } // #=============================================================================================# public void setSenderPassword(String senderPassword) { this.senderPassword = senderPassword; } // #=============================================================================================# public String getHostName() { return hostName; } // #=============================================================================================# public void setHostName(String hostName) { this.hostName = hostName; } // #=============================================================================================# public int getPortNumber() { return portNumber; } // #=============================================================================================# public void setPortNumber(int portNumber) { this.portNumber = portNumber; } // #=============================================================================================# public String getAttachmentPath() { return attachmentPath; } // #=============================================================================================# public void setAttachmentPath(String attachmentPath) { this.attachmentPath = attachmentPath; } // #=============================================================================================# public void sendMail(String receiverId) { try { // this below commented line for the HTML body text // MultiPartEmail htmlEmail = new HtmlEmail(); // OR // HtmlEmail email = new HtmlEmail(); MultiPartEmail email = new MultiPartEmail(); // setting the port number email.setSmtpPort(getPortNumber()); // authenticating the user email.setAuthenticator(new DefaultAuthenticator(getSenderID(), getSenderPassword())); // email.setDebug(true); email.setSSL(true); // setting the host name email.setHostName(getHostName()); // setting the rciever id email.addTo(receiverId); // check for user enterd cc or not if (getCc() != null) { // add the cc email.addCc(getCc()); } // check for user enterd bcc or not if (getBcc() != null) { // add the bcc email.addBcc(getBcc()); } // setting the sender id email.setFrom(getSenderID()); // setting the subject of mail email.setSubject(getSubject()); // setting message body email.setMsg(getBody()); // email.setHtmlMsg("<h1>"+getBody()+"</h1>"); // checking for attachment attachment if (getAttachmentPath() != null) { // add the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath(getAttachmentPath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); email.attach(attachment); } // send the email email.send(); // System.out.println("Mail sent!"); } catch (Exception e) { // System.out.println("Exception :: " + e); e.printStackTrace(); } }// sendmail() }// mail
package com.naveed.workingfiles; 导入org.apache.commons.mail.DefaultAuthenticator; 导入org.apache.commons.mail.Email附件; 导入org.apache.commons.mail.MultiPartEmail; 公营邮件{ 字符串senderID; 字符串senderPassword; 字符串主机名; int端口号; 字符串附件路径; 字符串主题; 弦体; 字符串cc; 字符串bcc; // #=============================================================================================# 公共字符串getBcc(){ 返回密件抄送; } // #=============================================================================================# 公共空白收信人(字符串密件抄送){ this.bcc=bcc; } // #=============================================================================================# 公共字符串getCc(){ 返回cc; } // #=============================================================================================# 公共void setCc(字符串cc){ this.cc=cc; } // #=============================================================================================# 公共字符串getBody(){ 返回体; } // #=============================================================================================# 公共体(字符串体){ 这个身体=身体; } // #=============================================================================================# 公共字符串getSubject(){ 返回主题; } // #=============================================================================================# public void setSubject(字符串主题){ this.subject=主语; } // #=============================================================================================# 公共字符串getSenderID(){ 返回senderID; } // #=============================================================================================# public void setSenderID(字符串senderID){ this.senderID=senderID; } 公共字符串getSenderPassword(){ 返回senderPassword; } // #=============================================================================================# public void setSenderPassword(字符串senderPassword){ this.senderPassword=senderPassword; } // #=============================================================================================# 公共字符串getHostName(){ 返回主机名; } // #=============================================================================================# public void setHostName(字符串主机名){ this.hostName=主机名; } // #=============================================================================================# public int getPortNumber(){ 返回端口号; } // #=============================================================================================# 公共无效集合端口号(int端口号){ this.portNumber=portNumber; } // #=============================================================================================# 公共字符串getAttachmentPath(){ 返回附件路径; } // #=============================================================================================# public void setAttachmentPath(字符串attachmentPath){ this.attachmentPath=attachmentPath; } // #=============================================================================================# public void sendMail(字符串receiverId){ 试一试{ //下面是HTML正文文本的注释行 //MultipartMail htmlEmail=新htmlEmail(); //或 //HtmlEmail电子邮件=新HtmlEmail(); MultipartMail电子邮件=新的MultipartMail(); //设置端口号 email.setSmtpPort(getPortNumber()); //验证用户身份 email.setAuthenticator(新的DefaultAuthenticator(getSenderID()), getSenderPa