faded8679

javamail

  是sun公司的用来处理邮件的api

  邮件协议:

    Smtp:简单邮件传输协议,在发送邮件时使用

    POP3:邮局协议,在接受邮件时使用

    IMAP:因特网消息访问协议,是POP3的扩展,功能更丰富

  传输过程:

              SMTP                                     POP3/IMAP

    zhangsan@qq.com                      qq邮件服务器                                        lisi@qq.com

需要的jar包:mail-1.4.7.jar

    import java.util.Properties;

    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMessage.RecipientType;

    public class Demo1 {
    public static void main(String[] args) throws Exception {
    //配置参数
    //创建属性对象
    Properties props = new Properties();
    //发送服务器需要身份认证
    props.put("mail.smtp.auth", "true");
    //设置邮件服务器主机名
    props.put("mail.host", "smtp.qq.com");
    //发送邮件协议名称
    props.put("mail.transport.protocol", "smtp");
    //端口号
    props.put("mail.smtp.port", "465");
    //qq邮箱需要ssl加密(端口,工厂类)
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    //创建会话对象
    Session session=Session.getInstance(props);
    //创建消息对象
    Message msg=new MimeMessage(session);
    //设置消息的标题
    msg.setSubject("测试java发送qq邮件的标题");
    //设置消息的内容
    msg.setText("今天下午测试邮件的内容");
    //设置消息的发送人
    msg.setFrom(new InternetAddress("1227074265@qq.com"));
    //设置消息的接收人
    msg.setRecipient(RecipientType.TO, new InternetAddress("2972509096@qq.com"));

    //创建发送消息对象
    Transport transport=session.getTransport();
    //设置发送地址,发送人账号和密码(授权码)
    transport.connect("smtp.qq.com","1227074265@qq.com","wsimkoryudxlicgd");
    //发送消息
    transport.sendMessage(msg, msg.getAllRecipients());
    //关闭
    transport.close();
    System.out.println("发送成功");
    }


    }
qq邮箱普通发送
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //配置参数
        //创建属性对象
        Properties props = new Properties();
        //发送服务器需要身份认证
        props.put("mail.smtp.auth", "true");
        //设置邮件服务器主机名
        props.put("mail.host", "smtp.qq.com");
        //发送邮件协议名称
        props.put("mail.transport.protocol", "smtp");
        //端口号
        props.put("mail.smtp.port", "465");
        //qq邮箱需要ssl加密(端口,工厂类)
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        //创建会话对象
        Session session=Session.getInstance(props);
        //创建消息对象
        Message msg=new MimeMessage(session);
        //设置消息的标题
        msg.setSubject("测试java发送qq邮件的标题");
        
        //多内容对象
        Multipart multipart=new MimeMultipart();
        
        //邮件文本
        BodyPart body=new MimeBodyPart();
        body.setText("测试java发送qq邮件的附件内容");
        //那文本添加到多内容对象中
        multipart.addBodyPart(body);
        
        //邮件附件
        BodyPart appendix=new MimeBodyPart();
        //数据源对象
        DataSource source =new FileDataSource("C:\\Users\\Public\\Pictures\\Sample Pictures\\b.JPG");
        //设置附件内容
        appendix.setDataHandler(new DataHandler(source));
        //设置文件的编码
        appendix.setFileName(MimeUtility.encodeText("a.jpg","utf-8",null));
        //把附件添加到多内容对象中
        multipart.addBodyPart(appendix);
        msg.setContent(multipart);
        
        
        
        //设置消息的发送人
        msg.setFrom(new InternetAddress("1227074265@qq.com"));
        //设置消息的接收人
        msg.setRecipient(RecipientType.TO, new InternetAddress("1367451525@qq.com"));

        //创建发送消息对象
        Transport transport=session.getTransport();
        //设置发送地址,发送人账号和密码(授权码)
        transport.connect("smtp.qq.com","1227074265@qq.com","wsimkoryudxlicgd");
        //发送消息
        transport.sendMessage(msg, msg.getAllRecipients());
        //关闭
        transport.close();
        System.out.println("发送成功");
        }
}
qq邮箱添加附件发送

 

分类:

技术点:

相关文章:

  • 2021-10-15
  • 2021-05-28
  • 2022-02-26
  • 2022-02-19
  • 2021-07-27
  • 2021-09-05
  • 2021-10-06
  • 2022-01-15
猜你喜欢
  • 2021-12-05
  • 2021-08-05
  • 2021-11-21
  • 2021-06-22
  • 2022-02-16
  • 2021-07-17
相关资源
相似解决方案