1.經過xml文件配置主要屬性:php
xml文件:test.xmlhtml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.126.com" /> <property name="port" value="25" /> <property name="username" value="dxxx@126.com" /> <property name="password" value="xxxxxxx" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean>
java發送代碼:java
a.text文字發送模式:web
//得到xml文件環境 ApplicationContext context = new ClassPathXmlApplicationContext("test.xml"); //取得配置 JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom("xxxxxx@126.com"); mail.setTo("ddddd@126.com"); mail.setSubject(" 測試spring Mail"); String text = "您購買了團購【韓式漢蒸,蒸出健康好膚色!】,嘀嗒券【19856121】已於2011年05月07日"; mail.setText(text); mailSender.send(mail);
b.html郵件發送模式:spring
//創建郵件消息,發送簡單郵件和html郵件的區別 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); //設置收件人,寄件人 messageHelper.setTo("sun111@163.com"); messageHelper.setFrom("webadmin@163.com"); messageHelper.setSubject("測試HTML郵件!"); //true 表示啓動HTML格式的郵件 messageHelper.setText("<html><head></head><body><h1>hello!!zhangjian</h1></body></html>",true); //發送郵件 senderImpl.send(mailMessage);
c.郵件中嵌套圖片測試
//得到xml文件環境 ApplicationContext context = new ClassPathXmlApplicationContext("test.xml"); //取得配置 JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); //創建郵件消息,發送簡單郵件和html郵件的區別 MimeMessage mailMessage = mailSender.createMimeMessage(); try { //注意這裏的boolean,等於真的時候才能嵌套圖片,在構建MimeMessageHelper時候,所給定的值是true表示啓用 multipart模式 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); //設置收件人,寄件人 messageHelper.setTo("dfdfd@126.com"); messageHelper.setFrom("ssss@126.com"); messageHelper.setSubject("測試郵件中嵌套圖片!!"); //true 表示啓動HTML格式的郵件 messageHelper.setText("<html><head></head><body><h1>hello!!zhangjian</h1><img src=/"cid:aaa/"/></body></html>",true); FileSystemResource img = new FileSystemResource(new File("c:/350.jpg")); messageHelper.addInline("aaa",img); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); }
2.經過java代碼設置屬性發送郵件:ui
//這個類主要是設置郵件 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.126.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("sssss@126.com"); mailInfo.setPassword("dddddfd");//您的郵箱密碼 mailInfo.setFromAddress("dfdfd@126.com"); mailInfo.setToAddress("vcvc@126.com"); mailInfo.setSubject("設置郵箱標題 如http://www.guihua.org 中國桂花網"); String content = "<div style=/"padding:20px;/"><a href=/"http://www.didatuan.com//">" + "<img src=/"http://www.didatuan.com/static/img/logo.gif/" style=/"border:none;/"></a>" + "<div style=/"width:100%; height:10px; border-bottom:1px solid #CCC;/"></div>" + "<h2 style=/"font-size:14px; color:#333; font-weight:bold; line-height:30px;/">Hi," + "<span style=/"color:#ff9900/">yue_861120</span>,您好!</h2>" + "<p style=/"font-size:12px; color:#666; line-height:20px;/">您購買了團購【韓式漢蒸,蒸出健康好膚色!】,嘀嗒券【19856121】已於2011年05月07日 13時52分消費。</p>" + "<p style=/"font-size:12px; color:#666; line-height:20px;/">【嘀嗒團客服】:400-000-1666</p>" + "<p style=/"font-size:12px; color:#666; line-height:20px;/">詳情請查詢:" + "<a href=/"http://www.didatuan.com//coupon/index.php/" target=/"_blank/" " + "style=/"color:#006699;/">http://www.didatuan.com/</a></p></div>"; mailInfo.setContent(content);//這個類主要來發送郵件 SimpleMailSender sms = new SimpleMailSender(); sms.sendTextMail(mailInfo);//發送文體格式 sms.sendHtmlMail(mailInfo);//發送html格式