表单提交eMail

来源:互联网 发布:linux 删除命令 编辑:程序博客网 时间:2024/06/14 07:21

这几天做了个表单提交邮件的小组件,供大家参考。

先简单介绍下:

一个jsp表单提交邮箱信息,注意这里我们把这些信息都只是封装起来,统一用我们的我们的中转邮箱去发送到收件人邮箱。

1.jsp 表单如下(js部分省了):

<body><center><div id="formDiv"><form name="mailform" id="mailform" method="POST" method="post"ENCTYPE="multipart/form-data"><input type='hidden' name='actiontype' value='yxjg'><table width="786" style="margin-top: 68px"><tr><td><b>用户名:</b></td><td><input type="text" name="userName" id="userName"style="width: 228px;" onfocus="userNameFocus()"onblur="userNameBlur()" /></td><td><div id="userNameCheck" class="tipDiv"></div></td></tr><tr><td><b>您的邮箱:</b></td><td><input type="text" name="userEmail" id="userEmail"style="width: 228px;" onfocus="userEmailFocus()"onblur="userEmailBlur()" /></td><td><div id="userEmailDiv" class="tipDiv"></div></td></tr><tr><td><b>您的电话:</b></td><td><input type="text" name="userPhone" id="userPhone"style="width: 228px;" onfocus="userPhoneFocus()"onblur="userPhoneBlur()" /></td><td><div id="userPhoneDiv" class="tipDiv"></div></td></tr><tr><td><b>收件人:</b></td><td><input type=text name="toName" style="width: 228px;"value="<%=toName%>" /></td><td><input type="hidden" name="toEmail" value="<%=toEmail%>" /></td></tr><tr><td><b>邮件主题:</b></td><td><input type="text" name="theme" id="theme"style="width: 228px;" onfocus="themeFocus()" onblur="themeBlur()" /></td><td><div id="themeDiv" class="tipDiv"></div></td></tr><tr><td><b>邮件正文:</b></td><td><textarea rows="12" cols="38" name="zhengwen"id="zhengwen"></textarea></td></tr><tr><td><b>邮件附件:</b></td><td><input type="file" name="fujian" id="fujian" /></td></tr><tr><td></td></tr><tr><td><input type='button' id='tijiao' value='提交' onclick='checkform()'></td></tr></table></form></div></center></body>


2.获取表单传来的参数摘要如下,调用sendMail方法:

String myName = "";String myEmail = "";//String myPhone = "";String subject = "";String msg = "";String attachment = "";List<FileItem> files = new ArrayList<FileItem>();String actionmethod = StringUtil.getParameter(request,"e");if ("yxjg".equals(actionmethod)) {FileItemFactory factory = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(factory);boolean isMultipart = upload.isMultipartContent(request);if(isMultipart){Iterator items = null;items = upload.parseRequest(request).iterator();while(items.hasNext()){FileItem item = (FileItem) items.next();if(item.isFormField()){if (item.getFieldName().equals("userEmail")) {myEmail = new String(item.getString().getBytes("8859_1"),"utf-8");} else if (item.getFieldName().equals("userName")) {myName = new String(item.getString().getBytes("8859_1"),"utf-8");} else if (item.getFieldName().equals("theme")) {subject = new String(item.getString().getBytes("8859_1"),"utf-8");} else if (item.getFieldName().equals("zhengwen")) {msg = new String(item.getString().getBytes("8859_1"),"utf-8");}}else if(!item.isFormField()){files.add(item);attachment = item.getName();}}}}

3.发送邮件:

public void sendMail(){String myEmailSMTPHost = "smtp.163.com";String myEmailAccount = "****@163.com";String myEmailPassword = "********";Properties props = new Properties(); // 参数配置props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTPprops.setProperty("mail.smtp.auth", "true"); // 需要请求认证Session session = Session.getDefaultInstance(props);session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 logtry{MimeMessage message = createMimeMessage(session, from,fromName, to, toName, subject, msg, attachmentPath);Transport transport = session.getTransport();transport.connect(myEmailSMTPHost, myEmailAccount, myEmailPassword);//transport.connect(myEmailAccount, myEmailPassword);transport.sendMessage(message, message.getAllRecipients());transport.close();} catch (Exception e){e.printStackTrace();}}
public MimeMessage createMimeMessage(Session session,String from,String fromName, String to,String toName,String subject,String msg,String attachmentPath) throws Exception{// 1. 创建一封邮件MimeMessage message = new MimeMessage(session);// 2. From: 发件人message.setFrom(new InternetAddress(from, fromName, "UTF-8"));// 3. To: 收件人(可以增加多个收件人、抄送、密送)message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to, toName, "UTF-8"));// 4. Subject: 邮件主题message.setSubject(subject, "UTF-8");// 设置消息内容Multipart multipart = new MimeMultipart();BodyPart messageBodyPart = new MimeBodyPart();messageBodyPart.setText(msg);multipart.addBodyPart(messageBodyPart);message.setContent(multipart);// 附件部分messageBodyPart = new MimeBodyPart();DataSource source = new FileDataSource(attachmentPath);messageBodyPart.setDataHandler(new DataHandler(source));String attachmentPathTrim = attachmentPath.trim();      String fileName = attachmentPathTrim.substring(attachmentPathTrim.lastIndexOf("\\")+1);messageBodyPart.setFileName(fileName);multipart.addBodyPart(messageBodyPart);message.setSentDate(new Date());message.saveChanges();return message;}




1 0