随着时代不断向前发展,科学技术的不断更新变革,经济发展水平的不断提升,人们对于新技术新产业的渴求越来越强烈。这也使得java的应用越发受人看重。那么,今天就主要来为大家介绍一下,如何在java中用exchange发送邮件,并且通过实例为大家展示多次发送的操作方法。
首先,我们可以了解一下,使用Exchange Server邮件服务器发送邮件的特殊之处在于,用户名username必须添加所在域的前缀。举个例子,如果要使用域domain中的用户demo@sample.com来发送邮件,那么登录名要改成domain\demo,而不是普通stmp服务器中的demo@sample.com。接下来就通过一个实际的代码为大家展示:
import java.util.Date; import java.util.Properties; import javax.mail.BodyPart; 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; public class Mailer { private String host; private String auth; private String username; private String domainUser; private String password; public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException { Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", auth); Session s = Session.getInstance(props); // s.setDebug(true); MimeMessage message = new MimeMessage(s); InternetAddress from = new InternetAddress(username); message.setFrom(from); InternetAddress[] Toaddress = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) Toaddress[i] = new InternetAddress(to[i]); message.setRecipients(Message.RecipientType.TO, Toaddress); if (cc != null) { InternetAddress[] Ccaddress = new InternetAddress[cc.length]; for (int i = 0; i < cc.length; i++) Ccaddress[i] = new InternetAddress(cc[i]); message.setRecipients(Message.RecipientType.CC, Ccaddress); } if (bcc != null) { InternetAddress[] Bccaddress = new InternetAddress[bcc.length]; for (int i = 0; i < bcc.length; i++) Bccaddress[i] = new InternetAddress(bcc[i]); message.setRecipients(Message.RecipientType.BCC, Bccaddress); } message.setSubject(subject); message.setSentDate(new Date()); BodyPart mdp = new MimeBodyPart(); mdp.setContent(content, "text/html;charset=utf-8"); Multipart mm = new MimeMultipart(); mm.addBodyPart(mdp); message.setContent(mm); message.saveChanges(); Transport transport = s.getTransport("smtp"); transport.connect(host, (null == domainUser) ? username : domainUser, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); return true; } public Mailer(String host, String auth, String domainUser, String username, String password) { super(); this.host = host; this.auth = auth; this.domainUser = domainUser; this.username = username; this.password = password; } }
调用:
new Mailer("smtp.sample.com", "true", "domain\\demo", "demo@sample.com", "password") .send(new String[] { "to@mail.com" }, null, null, "demo_title", "<h3>test</h3>");
然后我们再说一下多次发送邮件的方法,主要说三种,也就是controller,jsp以及js,通过实际的代码为大家展示。
1. controller:
// 发送邮件 @ResponseBody @RequestMapping("/fileUpdate2") public Object tpUpdate(String con, HttpSession session, AdminEmail adminEmail, Sysnotice sys, HttpServletRequest request, Model model) throws IOException { //System.out.println("进入con:"+con); String msg = null; try { // 邮件发送者 String addEmail = "zhongjingzhiku@163.com"; //发送人名称 String name = "*****平台"; // 根据邮件发送者,查询邮件发送者和邮箱授权码和邮件内容 adminEmail = notifyDownService.ademail(addEmail); //String con = request.getParameter("content"); //System.out.println("con:"+con); adminEmail.setContent(con); notifyDownService.insertcon(adminEmail); // System.out.println("+++++++++:"+adminEmail.getContent()); // 邮件接收者 String[] em = notifyDownService.email(); adminEmail.setToAddress(em); // System.out.println("adminEmail========:"+adminEmail.getToAddress().length); // 邮件标题 adminEmail.setTitle("文件更新通知公告"); boolean flag = JavaEmailSender.sendema(adminEmail, name); // 将信息插入到数据库 if (flag == true) { msg = "success"; //System.out.println("1.------------------------------------"); //session.setAttribute("msg",msg); //System.out.println("-------------------------:"+msg); //String conc = request.getParameter("content"); sys.setNoticecont(con); sys.setNoticetype(adminEmail.getTitle()); sys.setNoticetime(new Date()); notifyDownService.insertSys(sys); } else { msg = "fail"; session.setAttribute("msg", msg); } } catch (Exception e) { e.printStackTrace(); } // 在消息表里插入未读消息 mymessageService.addMessagebyType2(); return new JsonResult(msg); }
2. jsp:
<div class="form-x"> <div class="form-group"> <div class="label" style="width: 80px;float: left;line-height: 40px;"> <label>通知内容</label> </div> <div class="field" id="nr"> <textarea id="text" class="input" name="content" style=" height:90px;" data-validate="required:内容不为空" required maxlength="30">最多输入30字符</textarea> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label></label> </div> <input type="hidden" name="templateid" value="3057810"/><!-- 文件更新通知短信模板id --> <div class="field"> <button id="send" class="button bg-main" data-toggle="modal" data-target="#myModal" onclick="submit()">发送</button> </div> </div> </div>
3. js:
function submit() { var path = window.document.location.href.substring(0, window.document.location.href.indexOf(window.document.location.pathname)) + window.document.location.pathname.substring(0, window.document.location.pathname.substr(1) .indexOf('/') + 1); var con = $("#text") .val(); $.ajax( { type: "POST" , url: path + "/fileUpdate2" , data: { "con": con } , dataType: "json" , success: function (result) { var msg = result.data; if (msg == "success") { $.MsgBox.Alert("温馨提示", "发送成功!"); } else if (msg == "fail") { $.MsgBox.Alert("温馨提示", "发送失败!"); } } }); }
以上就是关于在java中用exchange发送邮件的具体操作方法,以及多次发送邮件的操作流程。想要了解更多java经典例子以及常见问题,敬请关注奇Q工具网。
推荐阅读: