代码片段
Posted boomoom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码片段相关的知识,希望对你有一定的参考价值。
发送邮件
package com.boomoom.store.utils; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * 发送邮件的工具类: * @author admin * */ public class MailUtils { public static void sendMail(String to,String code){ try { // 获得连接: Properties props = new Properties(); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "111"); } }); // 构建邮件: Message message = new MimeMessage(session); message.setFrom(new InternetAddress("[email protected]")); // 设置收件人: // TO:收件人 CC:抄送 BCC:暗送,密送. message.addRecipient(RecipientType.TO, new InternetAddress(to)); // 主题: message.setSubject("来自全球最大同性交友社区的激活邮件!"); // 正文: message.setContent("<h1>来自全球最大同性交友社区的激活邮件:请点击下面链接激活!</h1><h3><a href=‘http://localhost:8080/store_v2.0/UserServlet?method=active&code="+code+"‘>http://localhost:8080/store_v2.0/UserServlet?method=active&code="+code+"</a></h3>", "text/html;charset=UTF-8"); // 发送邮件: Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } } public static void main(String[] args) { MailUtils.sendMail("[email protected]", "sdfjklsdkljrsiduoigittery"); } }
以上是关于代码片段的主要内容,如果未能解决你的问题,请参考以下文章