JavaMail

一、邮件的相关概念

邮件协议。主要包括:

SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件

POP3协议:Post Office Protocol 3,即邮局协议的第三个版本,用于接收邮件

IMAP协议:Internet Message Access Protocol,即互联网消息访问协议,是POP3的替代协议

二、搭建James邮件服务器

 James是Apache的一个开源项目,纯Java实现

 搭建James服务器

 ① 下载apache-james-2.3.2.zip解压

 ② 运行bin目录下的run.bat即可启动服务器[Telnet  localhost 4555]

 ③ 通过appsjamesSAR-INFconfig.xml配置服务器

 注:先到bin下run一道。 放如非中文目录, 得再控制面板开启Telnet客户端

三、安装OutLook[邮件客户端]

产品秘钥:PQDV9-GPDV4-CRM4D-PHDTH-4M2MT

创建用户账号

一、使用telnet连接James的Remote Administration Tool

二、以管理员身份登录

三、使用adduser命令添加用户

1:输入telnet命令,指定连接的服务器地址和端口号

四、配置outlook邮件客户端

为了方便查看,可以配置Microsoft Outlook邮件客户端,保证James邮件服务器是启动状态,启动Microsoft Outlook.

选择“工具”->“选项”,打开“选项”面板。选择“邮件设置”并点击“电子邮件账户”,打开“账号设置”面板。在“电子邮件”选项卡下新建邮件账户

五、案例[搭建James邮件服务器]

需求说明:

在本机搭建James邮件服务器,自定义服务器的名称。

创建两个测试用户。

在Microsoft Outlook中配置其中一个测试用户为Outlook邮件账户

六、使用JavaMail发送电子邮件(案例)

 需求:

使用JavaMail技术,实现从A账户给B账户发送一封电子邮件,标题为“会议通知”,邮件内容为“XX你好!请于明天下午16:00 准时到B01会议室召开技术讨论会。”通过Outlook 客户端查看邮件程序发送的邮件是否发送成功

关键代码:

创建一个类EmailAuthenticator并继承自Authenticator,并植入用户名和密码

package cn.happy.tets;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
  private String mailServer,from,to,mailSubject,mailContent;
  private String username,password;
  public Mail(){
      //设置邮件信息
      //进行认证登录的用户名
      username="cmy@mail.com";
      //认证密码
      password="cmy";
      //认证的邮箱对应的邮件服务器
      mailServer="192.168.8.71";
      //发件人信息
      from="cmy";
      //收件人信息
      to="wj@mail.com";
      //邮件标题
      mailSubject="呵呵,嘻嘻,哈哈";
      //邮件内容
      mailContent="我去";
  }
  //设置邮件服务器
@SuppressWarnings("static-access")
public  void send(){
      Properties prop=System.getProperties();
      //指定邮件server
      prop.put("mail.smtp.host", mailServer);
      
      //是否开启认证
      prop.put("mail.smtp.auth", "true");
      
      //smtp协议的
      prop.put("mail.smtp.port", "25");
      //产生Session服务
      EmailAuthenticator mailauth=new EmailAuthenticator(username, password);
      Session mailSession=Session.getInstance(prop,(Authenticator)mailauth);
       try {
           //封装Message对象
           Message message=new MimeMessage(mailSession);
           
           message.setFrom(new InternetAddress(from)); //发件人
           message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//收件人
           message.setSubject(mailSubject);
           //设置内容(设置字符集处理乱码问题)
           message.setContent(mailContent,"text/html;charset=gbk");
           message.setSentDate(new Date());
           //创建Transport实例,发送邮件
           Transport tran=mailSession.getTransport("smtp");
           tran.send(message,message.getAllRecipients());
           tran.close();
           
        } catch (Exception e) {
            e.printStackTrace();
        }
  }
}
package cn.happy.tets;

public class Test {
    public static void main(String[] args) {
        Mail mail=new Mail();
        mail.send();
        System.out.println("success!");
    }

}
public class MailWithAttachment {
    private JavaMailSender mailSender; //必须使用 JavaMailSender
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
     
    public void send() throws MessagingException,IOException{
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setFrom("hq@mail.com");
        helper.setTo("wj@mail.com");
         
        helper.setSubject("接好了");
        helper.setText("好看不");
        //添加附件1
        ClassPathResource file1 = new ClassPathResource(
                                        "/cn/bdqn/attachfiles/test.doc");
        helper.addAttachment(file1.getFilename(), file1.getFile());
        //添加附件2:附件的文件名为中文时,需要对文件名进行编码转换,解决乱码问题
        ClassPathResource file2 = new ClassPathResource(
                                        "/cn/bdqn/attachfiles/附件测试文件.doc");
        helper.addAttachment(MimeUtility.encodeWord(file2.getFilename()),file2.getFile());
        mailSender.send(mimeMessage);
    }
}
public class MailTest {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         
        /*测试带附件的邮件*/
        try{
            MailWithAttachment mailWithAttach = (MailWithAttachment)context.getBean("mailWithAttachment");
            mailWithAttach.send();
        }catch(Exception e){
            System.out.print(e.toString());
        }
    }
}  
public class MailTest {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         
        /*测试带附件的邮件*/
        try{
            MailWithAttachment mailWithAttach = (MailWithAttachment)context.getBean("mailWithAttachment");
            mailWithAttach.send();
        }catch(Exception e){
            System.out.print(e.toString());
        }
    }
}  
原文地址:https://www.cnblogs.com/lizeyang/p/6193229.html