1.通过xml文件配置主要属性:
xml文件:test.xml
- <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发送代码:
a.text文字发送模式:
- //获得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邮件发送模式:
- //建立邮件消息,发送简单邮件和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代码设置属性发送邮件:
- //这个类主要是设置邮件
- 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=/"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格式