Maile邮件工具类

电子邮件工具类

package util;
import java.util.Properties;
import javax.mail.Address;
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;

/**
 * 电子邮件工具类
 * @author Administrator
 *
 */
public class MailUtil {


    /**
     * 发送电子邮件
     * @param addr 收件人地址
     * @param subject 主题
     * @param text 内容
     * @throws MessagingException
     */
    public static void sendMail(String addr,String subject,String text) throws MessagingException{
        Properties props=new Properties();
        props.put("mail.smtp.host","smtp.sina.com");
        props.put("mail.smtp.auth","true");
        Session session=Session.getInstance(props);
        //构造信息体 
        MimeMessage message =new MimeMessage(session);
         //发件地址 
        Address address = new InternetAddress("wwwitcastcn@sina.com"); 
        message.setFrom(address);
        //收件地址 
        Address toAddress = new InternetAddress(addr); 
        message.setRecipient(MimeMessage.RecipientType.TO, toAddress);
        //主题 
        message.setSubject(subject);
        //正文 
        message.setText(text);
        message.saveChanges();
        Transport transport = session.getTransport("smtp"); 
        transport.connect("smtp.sina.com", "wwwitcastcn@sina.com", "itcast"); //发送 
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }

}

附文件图片的邮件

package demo;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class JavaMail03Test {

    @Test
    public void testJavaMail() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");


        JavaMailSender sender = (JavaMailSender) ac.getBean("mailSender");       //得到邮件的发送对象,专门用于邮件发送

        //发送一个允许带图片,同时带附件的邮件
        MimeMessage message = sender.createMimeMessage();//创建一封允许带图片,同时带附件的邮件对象

        //为了更好的操作MimeMessage对象,借用一个工具类来操作它
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        //通过工具类设置主题,内容,图片,附件
        helper.setFrom("itheima14@163.com");
        helper.setTo("3462420264@qq.com");
        helper.setSubject("这是一个请求");
        helper.setText("<html><head></head><body><h1>hello!!baby </h1>"
                    +"<a href=http://www.itheima.com>哈哈哈哈</a>"  + "<img src=cid:image/></body></html>",true);//第二个参数说明内容要解析为html代码

        //添加图片
        FileSystemResource resource = new FileSystemResource(new File("E:\\01分配权限原理分析.png"));
        helper.addInline("image", resource);

        sender.send(message);


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值