zoukankan      html  css  js  c++  java
  • 发送邮件工具方法

    import java.util.Properties;
    import java.util.UUID;
    
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import cn.itcast.estore.domain.User;
    
    /**
     * 发送邮件 工具方法
     * 
     * @author seawind
     * 
     */
    public class MailUtils {
    	// 生成激活码
    	public static String generateActivecode() {
    		return UUID.randomUUID().toString();
    	}
    
    	// 发送邮件
    	public static void sendMail(Message message, Session session)
    			throws Exception {
    		Transport transport = session.getTransport();
    		transport.connect("service", "111");
    		transport.sendMessage(message, message.getAllRecipients());
    	}
    
    	// 生成邮件
    	public static Message generateMessage(Session session, User user)
    			throws Exception {
    		MimeMessage message = new MimeMessage(session);
    		// 邮件头
    		message.setFrom(new InternetAddress("service@estore.com"));// 发件人
    		message.setRecipient(Message.RecipientType.TO, new InternetAddress(user
    				.getEmail())); // 收件人
    		message.setSubject("ESTORE商城 激活邮件");
    		// 邮件头
    		message
    				.setContent(
    						"<h2>欢迎"
    								+ user.getUsername()
    								+ "注册Estore商城,这里可以购买您需要商品!</h2><h3>请于2小时内点击下面链接完成账户激活:</h3><a href='http://www.estore.com/active?activecode="
    								+ user.getActivecode()
    								+ "'>http://www.estore.com/active?activecode="
    								+ user.getActivecode() + "</a>",
    						"text/html;charset=utf-8");
    		return message;
    	}
    
    	// 创建会话
    	public static Session createSession() {
    		Properties properties = new Properties();
    		properties.put("mail.transport.protocol", "smtp");
    		properties.put("mail.smtp.host", "localhost");
    		properties.put("mail.smtp.auth", "true");
    
    		Session session = Session.getInstance(properties);
    		return session;
    	}
    }

  • 相关阅读:
    SDWebImage内存性能优化
    dataGridView1数据导出
    listView数据导出
    sqlserver定时备份
    C#-IniFiles文件配置连接数据库
    (转)线程同步详解
    C#工具类:Json操作帮助类(转载)
    c#实现验证码功能
    屏蔽F12审查元素,禁止使用右键菜单
    c# excel如何导入到sqlserver数据库
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239927.html
Copyright © 2011-2022 走看看