jmail 发送 邮件 进行 密码找回
---------------------------------------------------------------------------------------------------------------------------
package com.user.service.impl; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import com.fusionability.user.service.EmailService; import com.fusionability.web.bean.User; public class EmailServiceImpl implements EmailService,InitializingBean{ private static Logger logger = Logger.getLogger(EmailServiceImpl.class); private EmailInfo emailInfo ; public class EmailInfo { public String smtpServer = "smtp.sina.com"; public boolean smtpAuth = true; public String email = "knight_black_bob@sina.com"; public String loginName = "knight_black_bob"; public String passwd = ""; } private MimeMessage message; private Session s; public static String KEY_SMTP = "mail.smtp.host"; public static String KEY_PROPS = "mail.smtp.auth"; private EmailService emailService; public void retrievePasswordEmail(User user) throws Exception{ sendEmail(user.getUsername(), "找回密码", getRetrievePasswordEmail(user)); } public void sendEmail(String mailbox, String title, String message) throws Exception { sendEmail(new String[] { mailbox }, title, message); } public void sendEmail(String[] mailbox, String title, String message) throws Exception { try { InternetAddress from = new InternetAddress(this.emailInfo.email); this.message.setFrom(from); // 收件人 InternetAddress[] tos = new InternetAddress[mailbox.length]; for (int i = 0; i < mailbox.length; i++) tos[i] = new InternetAddress(mailbox[i]); this.message.setRecipients(Message.RecipientType.TO, tos); // 邮件标题 this.message.setSubject(title); // 邮件内容,也可以使纯文本"text/plain" this.message.setContent(message, "text/html;charset=GBK"); this.message.saveChanges(); Transport transport = s.getTransport("smtp"); // smtp验证,就是你用来发邮件的邮箱用户名密码 transport.connect(this.emailInfo.smtpServer, this.emailInfo.loginName, this.emailInfo.passwd); // 发送 transport.sendMessage(this.message, this.message.getAllRecipients()); transport.close(); } catch (AddressException e) { logger.warn("", e); } catch (MessagingException e) { logger.warn("", e); } } private String getRetrievePasswordEmail(User user){ StringBuffer email = new StringBuffer(); email.append(" -.- "); return email.toString(); } //get set public EmailService getEmailService() { emailInfo = new EmailInfo(); return emailService; } public void setEmailService(EmailService emailService) { this.emailService = emailService; } }
public void afterPropertiesSet() throws Exception { //String ec = sysConfigService.getEmailConfig(); if (true) { //smtp.sina.com,knihgt_black_bob@sina.com,knihgt_black_bob,aaaa //String[] ss = ec.split(","); this.emailInfo.smtpServer = "smtp.sina.com"; this.emailInfo.email = "knihgt_black_bob@sina.com"; this.emailInfo.loginName = "knihgt_black_bob"; this.emailInfo.passwd = "aaaa"; } Properties props = System.getProperties(); props.setProperty(KEY_SMTP, this.emailInfo.smtpServer); props.put(KEY_PROPS, this.emailInfo.smtpAuth); s = Session.getInstance(props); message = new MimeMessage(s); }
html 邮件 格式 注意事项:
1、全局规则之一,不要写<style>标签、不要写class,所有CSS都用style属性,什么元素需要什么样式就用style写内联的CSS。
2、全局规则之二,少用图片,邮箱不会过滤你的img标签,但是系统往往会默认不载入陌生来信的图片,如果用了很多图片的邮件,在片没有载入的情况下,丑陋无比甚至看不清内容,没耐心的用户直接就删除了。图片上务必加上alt。
3、不要在style里面写float、position这些style,因为会被过滤。那么如何实现左右布局或者更复杂的布局呢?用table。
4、style内容里面background可以设置color,但是img会被过滤,就是说不能通过CSS来设置背景图片了。但是有一个很有意思的元素 属性,也叫background,里面可以定义一个图片路径,这是个不错的替代方案,虽然这样功能有限,比如无法定位背景图片了,有总比没有好。例如要给 一个单元格加一个背景,必须这样写:
<td background=”http://image1.koubei.com/images/common/logo_koubei.gif”></td>
5、div模式的邮箱不支持flash,iframe模式的有待验证。
最后提一句,sohu的邮箱很怪异,会在每个文本段后面加一个空格,导致原本正常的排版一行放不下而换行,从而使某些布局错乱。所以,如果你要兼容sohu邮箱的话,遇到一些紧凑的布局就要格外小心了,尽量减少文本段的数量,留足宽度。
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!