strus2 java发送邮件实例

本文提供了一个使用Java发送邮件的示例代码,包括通过不同邮箱服务商(如163和QQ邮箱)发送纯文本和HTML格式邮件的功能,并支持附件、抄送及密送。

java代码 package com.sj.action; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; @SuppressWarnings("serial") public class MailAction extends ActionSupport implements Preparable{ private String mail; // 发件人的账号 private String password; // 发件人的密码 private String to; // 收件人的地址 private String cs; // 抄送地址 private String ms; // 密送地址 private String subject; // 发送主题 private String content; // 发送内容 private String fj; // 附件内容 final String HOST_KEY="mail.smtp.host";// final String USER_KEY="mail.smtp.user";// final String PWD_KEY="mail.smtp.pwd";// final String AUTH_KEY="mail.smtp.auth";// final String AUTH_VALUE="true";// //清除多个的提示信息 public void prepare() throws Exception { clearErrorsAndMessages(); } //发送邮件实例 public String sendMail(){ return "mailinit"; } //发送邮件的方法 public String sendEmail() throws MessagingException, UnsupportedEncodingException{ Properties props=new Properties(); //判断是哪个邮箱 String HOST_VALUE=""; //smtp.163.com if(mail.contains("@163.com")){ HOST_VALUE="smtp.163.com"; } if(mail.contains("@qq.com")){ HOST_VALUE="smtp.qq.com"; } props.put(HOST_KEY, HOST_VALUE); //发件人的服务器 props.put(USER_KEY, this.mail); // 发件人的账号 props.put(PWD_KEY, this.password); //发件人的密码 props.put(AUTH_KEY, AUTH_VALUE); // 自动发送 Session session = Session.getDefaultInstance(props); Message message=new MimeMessage(session); //message.setSubject(new String(subject.getBytes("GB2312"),"ISO-8859-1")); //发送主题 message.setSubject(subject); message.setSentDate(new Date()); // 发送日期 message.setFrom(new InternetAddress(mail)); //发送邮件者 message.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); //接受邮件着 // 抄送地址的判断 if(!cs.equals("")){ message.addRecipient(Message.RecipientType.CC,new InternetAddress(cs)); } // 密送送地址的判断 if(!ms.equals("")){ message.addRecipient(Message.RecipientType.BCC,new InternetAddress(ms)); } //内容的处理 Multipart mPart=new MimeMultipart(); MimeBodyPart bodyPart=new MimeBodyPart(); String mail_type="text/plain"; if(content.startsWith("<html>")||content.startsWith("<HTML>")){ mail_type="text/html;"; } bodyPart.addHeader("Content-Type", mail_type); bodyPart.setHeader("Content-Transfer-Encoding", "base64"); // 指定使用base64编码 bodyPart.setText(content); mPart.addBodyPart(bodyPart); //附件的操作 if(!fj.equals("")){ MimeBodyPart bdp=new MimeBodyPart(); FileDataSource fds=new FileDataSource(fj); bdp.setDataHandler(new DataHandler(fds)); bdp.setFileName(MimeUtility.encodeText(fds.getName())); mPart.addBodyPart(bdp); } message.setContent(mPart); // 内容的处理 message.saveChanges(); Transport trans=session.getTransport("smtp"); trans.connect(HOST_VALUE,mail,password); // 连接 trans.sendMessage(message, message.getAllRecipients()); trans.close(); addActionMessage(getText("admin_querysucc")); return "succ"; } /// 以下是get和set 方法 public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getCs() { return cs; } public void setCs(String cs) { this.cs = cs; } public String getMs() { return ms; } public void setMs(String ms) { this.ms = ms; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getFj() { return fj; } public void setFj(String fj) { this.fj = fj; } }

JSP代码

<%@ page contentType="text/html; charset=gbk"%> <%@include file="../common/admin_head.jsp"%> <%@taglib prefix="s" uri="/struts-tags"%> <%@ taglib uri="/WEB-INF/FCKeditor.tld" prefix="fck"%> <%@taglib prefix="sj" uri="/sjep"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link href="<%=basepath%>/css/admin.css" rel="stylesheet" type="text/css" /> <mce:script type="text/javascript" src="../js/sujian.js" mce_src="js/sujian.js"></mce:script> <mce:script type="text/javascript" src="../fckeditor.js" mce_src="fckeditor.js"></mce:script> <title> <s:text name="admin_user_title"></s:text> </title> <mce:script type="text/javascript"><!-- function trim(str){ if(str==null||str=="") return ""; return str.replace(/^/s+/g, '').replace(//s+$/g, ''); }; function check(){ with(document.forms[0]){ if(trim(mail.value)==""){ alert('发件人账号必须输入,请输您入!'); mail.focus(); return false; } //电子邮件格式的判断 if(mail.value.length!=0 &&!checkEmail(mail.value)){ mail.focus(); return false; } //发件人的密码盘空 if(trim(password.value)==""){ alert('发件人密码必须输入,请您输入!'); password.focus(); return false; } //发件人地址的判空 if(trim(to.value)==""){ alert('发件人地址必须输入,请您输入!'); to.focus(); return false; } //发件人地址电子邮件格式的判断 if(to.value.length!=0 &&!checkEmail(to.value)){ to.focus(); return false; } //内容的判断 if(trim(content.value)==""){ alert('内容必须输入,请您输入!'); content.focus(); return false; } return true; } } function save(){ with(document.forms[0]){ if(!check()){ return false; } document.forms[0].submit(); } } // 添加抄送 function chaosong(){ with(document.forms[0]){ var tr1 = document.getElementById('tr1'); tr1.style.display="block"; } } // 取消抄送 function qchaosong(){ with(document.forms[0]){ var tr1 = document.getElementById('tr1'); tr1.style.display="none"; cs.value=""; } } //添加密送 function misong(){ with(document.forms[0]){ var tr2 = document.getElementById('tr2'); tr2.style.display="block"; } } //取消密送 function qmisong(){ with(document.forms[0]){ var tr2 = document.getElementById('tr2'); tr2.style.display="none"; ms.value=""; } } function reset(){ with(document.forms[0]){ mail.value=""; password.value=""; to.value=""; cs.value=""; ms.value=""; subject.value=""; content.value=""; } } // --></mce:script> </head> <body> <center> <div class="titleText" align="center"> <s:text name="admin_mail"/> </div> <s:form action="sendEmailAction.action" id="form0" onsubmit="return save();"> <table class="table" align="center" width="80%"> <tr> <th colspan="4" class="center">发件人 </th> </tr> <tr> <th colspan="4" class="center"> <font class="font12y">注意:发件人请使用163邮箱或者腾讯QQ邮箱发送邮件</font> </th> </tr> <tr> <th>账号</th> <td> <s:textfield id="mail" name="mail"/><font class="font12y">*</font> </td> <th>密码</th> <td> <s:textfield id="password" name="password"/><font class="font12y">*</font> </td> </tr> </table> <table align="center" class="table" border="1" width="80%"> <tr> <th>收件人</th> <td> <s:textfield id="to" name="to" size="60"/><font class="font12y">*</font> <a href="javascript:chaosong();" mce_href="javascript:chaosong();"> 添加抄送 </a> <a href="javascript:misong();" mce_href="javascript:misong();"> 添加密送 </a> </td> </tr> <tr style="display:none" mce_style="display:none" id="tr1"> <th>抄送 </th> <td> <s:textfield id="cs" name="cs" size="60"/> <a href="javascript:qchaosong();" mce_href="javascript:qchaosong();"> 取消抄送 </a> </td> </tr> <tr style="display:none" mce_style="display:none" id="tr2"> <th>密送</th> <td> <s:textfield id="ms" name="ms" size="60"/> <a href="javascript:qmisong();" mce_href="javascript:qmisong();"> 取消密送 </a> </td> </tr> <tr> <th>主题</th> <td> <s:textfield id="subject" name="subject" size="60"/><font class="font12y">*</font> </td> </tr> <tr> <th>内容:</th> <td> <s:textarea id="content" name="content" rows="15" cols="100%" /> </td> </tr> <tr> <th>附件:</th> <td> <input name="fj" type="file" title="浏览"/> </td> </tr> <tr> <th colspan="3" class="center"> <input type="button" value=" 发 送 " size="35" onclick="save();"> <input type="button" value="重 新 填 写 " size="35" onclick="reset();"> </th> </tr> </table> </s:form> </center> </body> </html>

struts 配置文件代码

<!-- 发送邮件实例 --> <action name="mailAction" class="com.sj.action.MailAction" method="sendMail"> <result name="mailinit">/admin/sendMail.jsp</result> <interceptor-ref name="loginedCheck"/> <interceptor-ref name="defaultStack"/> </action> <action name="sendEmailAction" class="com.sj.action.MailAction" method="sendEmail"> <result name="succ">/admin/sendMail.jsp</result> <interceptor-ref name="loginedCheck"/> <interceptor-ref name="defaultStack"/> </action>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值