我设计了一个联系表格,我需要收到来自"联系我们的邮件。页面,关于特定的电子邮件ID。
这是我的表格:
contact.html
<form action="contactservlet" method="post">
<h4>Get in <i>Touch</i></h4>
<input type="text" id="name" name="name" placeholder="NAME" class="input-style" />
<input type="email" id="emailid" name="emailid" placeholder="EMAIl" oninput="sendRequest('GET','Checkemailid',1)" required="required" class="input-style" />
<input type="text" id="mobno" name="mobno" placeholder="Enter Mobile No." required="required" class="input-style"/>
<textarea type="text" id="message" name="message" placeholder="MESSAGE" required="required" class="input-style"></textarea>
<div class="contact-btn">
<a title="" href="#"><input type="submit" id="submit" value="Submit"/></a>
</div>
</form>
我不知道如何撰写contactservlet.java
。
答案 0 :(得分:0)
<强>已更新强>
试试这个,
ContactUs.html
<form action="ContactServlet" method="post">
<h4>Get in <i>Touch</i></h4>
<input type="text" id="name" name="name" placeholder="Name" />
<input type="email" id="emailId" name="emailId" placeholder="Email" />
<input type="text" id="mobileNo" name="mobileNo" placeholder="Enter Mobile No." />
<textarea type="text" id="message" name="message" placeholder="Message"></textarea>
<input type="submit" id="submit" value="Submit"/>
</form>
对于发送邮件,我们使用了JavaMail API。 Download和Documentation。
ContactServlet.java -> doPost()
//This data will be added to your Database using DAO Layer....
String name=request.getParameter("name");
String emailId=request.getParameter("emailId");
Long mobno=Long.parseLong(request.getParameter("mobileNo"));
String message= request.getParameter("message");
//Call Some function that do database storage.....
//Sending Mail Through Gmail SMTP
//Separate this code at DAO/Service Layer - - -
final String gmailUsername = "somethin@gmail.com";
final String gmailPassword = "somethin@123";
try{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(gmailUsername, gmailPassword);
}
});
Message mailMessage = new MimeMessage(session);
mailMessage.setFrom(new InternetAddress("somethin@gmail.com"));
mailMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(emailId));
mailMessage.setSubject("Type Subject : ");
mailMessage.setText("Type LONGGGG Message Here......");
Transport.send(mailMessage);
} catch(MessagingException e){
e.printStackTrace();
}