JavaMail发送邮件

序:
最近谈了个单子,做某Model网站,有发送EDM的需求、爬虫的需求、支付宝…为了帮甲方赶时间,签约之前团队分工花了几天研究实现了部分功能。

几个功能倒是研究出来了,甲方借口一大堆,迟迟无法签约,伙伴们情绪低落…看来又帮人白做了一次前置需求和合同演练。刹那间万马奔腾,默默发克油~~

JavaMail 发送邮件

本文只谈邮件,爬虫(基于Python)等其他的研究成果之后再发文聊。

闲话不说,上代码

package com.facecto.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.Properties;

以上是依赖包

@RunWith(SpringRunner.class)
@SpringBootTest

public class DemoApplicationTests {
    @Autowired
    private JavaMailSender mailSender;

第一个测试方法

这个是最基本的发送邮件代码

    @Test
    public  void send01() throws Exception{
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        helper.setSubject("测试发送邮件");
        helper.setText("测试发送邮件");
        mailSender.send(mimeMessage);
    }

第二个测试方法

这个方法使用JavaMailSenderImpl发送

    @Test
    public  void send02() throws  Exception{
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        message.setSubject("主题:简单邮件");
        message.setText("测试邮件内容");
        message.setReplyTo("[email protected]");
        JavaMailSenderImpl sendimpl = new JavaMailSenderImpl();
        sendimpl.setHost("smtp.qq.com");
        sendimpl.setDefaultEncoding("UTF-8");
        sendimpl.setPassword("password");
        sendimpl.setPort(25);
        sendimpl.setUsername("[email protected]");
        Properties prop = new Properties();
        prop.put("mail.smtp.auth","true");
        prop.put("mail.smtp.timeout","25000");
        sendimpl.setJavaMailProperties(prop);
        sendimpl.send(message);
    }

第三种方法

这种是EDM的发送方法

    @Test
    public void Send03() throws  Exception{
        String to = "[email protected]";
        String from = "[email protected]";
        final String username = "[email protected]";
        final String password = "password";
        String host = "smtp.facecto.com";
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "25");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
        message.setSubject("标题");
        MimeMultipart multipart = new MimeMultipart("related");
        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "

Hello,这里是html

"; messageBodyPart.setContent(htmlText, "text/html"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource( "/web/mail/111.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", ""); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); }

代码写完了,不对,还有最后一行

}

分享完了,也自己做个笔记。
本文还发到了www.facecto.com,技术需要大家一起玩。

你可能感兴趣的:(JavaMail发送邮件)