07-Mail sending

email sending

introduce

  • To realize the mail function on the network, there must be a dedicated mail server.

  • These mail servers are similar to post offices in real life. They are mainly responsible for receiving mail delivered by users and delivering the mail to the email recipient's e-mail box.

  • SMTP server address: generally smtp.xxx.com, such as 163 mailbox is smtp.163.com, qq mailbox is smtp.qq.com.

  • To obtain an e-mail address (E-mail address), an application must be made on the mail server. For example, if we want to use QQ mailbox, we need to open the mailbox function.

Transfer Protocol

SMTP protocol: send mail:

  • We usually refer to the server that processes the user's smtp request (mail sending request) as the SMTP server (mail sending server).

POP3 protocol: receiving mail:

  • We usually refer to the server that processes the user's pop3 request (mail receiving request) as the POP3 server (mail receiving server).

Sending and receiving principle

insert image description here

  1. Dakuangshen connects to the smtp server through the smtp protocol, and then sends an email to Netease's mail server.
  2. Netease analysis found that it is necessary to go to QQ's mail server and forward the mail to QQ's smtp server through the smtp protocol.
  3. QQ stores the received emails in the space of the email account [email protected].
  4. Xiaokuangshen connects to the Pop3 server to receive emails through the Pop3 protocol.
  5. Take out the mail from the space of the mail account [email protected].
  6. The Pop3 server sends the retrieved emails to Xiaokuangshen.

insert image description here

01-servlet code

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
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.util.Properties;

public class sendMail {
    
    
    public static void main(String[] args)throws Exception {
    
    
        Properties properties = new Properties();
        properties.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
        properties.setProperty("mail.transport.protocol","smtp");//设置QQ邮件发送协议
        properties.setProperty("mail.smtp.auth","true");//验证用户名和密码


        //关于QQ邮箱,还要设置SSL加密
        MailSSLSocketFactory mailSSL = new MailSSLSocketFactory();
        mailSSL.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable","true");
        properties.put("mail.smtp.ssl.socketFactory",mailSSL);

        //使用Java发送邮件的步骤


        //1.创建定义整个程序所需的环境信息Session对象

        //qq独有
        Session session=Session.getDefaultInstance(properties, new Authenticator() {
    
    
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
    
    
                return new PasswordAuthentication("[email protected]","wemrbvbtyaniebab");
            }
        });

        //2.通过session得到transport对象

        Transport ts = session.getTransport();

        //3.使用邮箱的用户名和授权码连上邮件服务器

        ts.connect("smtp.qq.com","[email protected]","wemrbvbtyaniebab");

        //4.创建邮件
        MimeMessage message = new MimeMessage(session);
        //指明发送邮件人
        message.setFrom(new InternetAddress("[email protected]"));
        //指明邮件的收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
        //指明邮件的标题
        message.setSubject("只包含文本的内容");
        //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("src\\resources\\bz.jpg"));
        image.setDataHandler(dh);
        image.setContentID("bz.jpg");
        //邮件的文本内容
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<h1>你好啊</h1><img src='cid:bz.jpg'>","text/html;charset=UTF-8");
        //描述数据关系
        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(image);
        mimeMultipart.addBodyPart(text);
        mimeMultipart.setSubType("related");//mixed最大
        //保存到数据中
        message.setContent(mimeMultipart);
        message.saveChanges();
        //5.发送邮件

        ts.sendMessage(message,message.getAllRecipients());
        ts.close();
    }
}

``

Guess you like

Origin blog.csdn.net/qq_52117201/article/details/129404534