• javaMail


    知识点:javaMail api包含Session,Message,Transport,InternetAddress
    Session:封装一组与邮件配置有关的方法,通过java.util.Properties或得配置信息,通过getdefaultInstance()获得邮件会话.
    Message:邮件信息类,主要方法:
    setFrom();setSubject();setContent();setSentDate();setText();addRecipient(收件人类型,收件邮箱);
    Transport:邮件传输类Transport.send(message);

    InternetAddress:用户的邮箱地址
    1.发送纯文本文件

    package main;
    
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class send2 {
        public void SendMail() throws MessagingException{
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("ppag38615f19c8c8@sohu.com(发送邮箱)", "发送邮箱密码");
                }
            };//获取认证验证
    
            Properties prop=new Properties();
            prop.setProperty("mail.host","smtp.sohu.com");
            prop.setProperty("mail.smtp.auth", "true");
            prop.setProperty("mail.transport.protocol", "SMTP");//邮件配置信息
            Session s=Session.getDefaultInstance(prop,auth);
    
            MimeMessage me=new MimeMessage(s);
            InternetAddress to=new InternetAddress("1278808836@qq.com");
            me.addRecipient(Message.RecipientType.TO, to);
            me.setFrom(new InternetAddress("ppag38615f19c8c8@sohu.com")); // 设置发送者
            //me.setContent("hello world","text/html;utf-8");
            me.setSubject("主题");
            me.setText("hello");
    
            Transport t=s.getTransport("smtp");
            t.send(me);
    
        }
        public static void main(String[] args) throws MessagingException {
            new send2().SendMail();
        }
    
    }
    

    2.发送html格式的文件

    package main;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class send2 {
        public void SendMail() throws MessagingException, IOException{
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("ppag38615f19c8c8@sohu.com", "发送密码");
                }
            };//获取认证验证
    
            Properties prop=new Properties();
            prop.setProperty("mail.host","smtp.sohu.com");
            prop.setProperty("mail.smtp.auth", "true");
            prop.setProperty("mail.transport.protocol", "SMTP");//邮件配置信息
            Session s=Session.getDefaultInstance(prop,auth);
    
            MimeMessage me=new MimeMessage(s);
            InternetAddress to=new InternetAddress("1278808836@qq.com");
            me.addRecipient(Message.RecipientType.TO, to);
            me.setFrom(new InternetAddress("ppag38615f19c8c8@sohu.com")); // 设置发送者
            //me.setContent("hello world","text/html;utf-8");
            me.setSubject("主题");
            //me.setText("hello");
            String source="C:\Users\佩\Desktop\2.html";
            String content="";
            String line=null;
            BufferedReader br=new BufferedReader(new FileReader(source));
            while((line=br.readLine())!=null){
                content+=line;
            }
            br.close();
            me.setContent(content, "text/html;charset=utf-8");
            Transport t=s.getTransport("smtp");
            t.send(me);
    
        }
        public static void main(String[] args) throws MessagingException, IOException {
            new send2().SendMail();
        }
    
    }
    

    3.发送带附件的邮箱

    package main;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    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.MimeMultipart;
    import javax.mail.internet.MimeUtility;
    
    public class send2 {
        public void SendMail() throws MessagingException, IOException{
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("ppag38615f19c8c8@sohu.com", "04143114wp");
                }
            };//获取认证验证
    
            Properties prop=new Properties();
            prop.setProperty("mail.host","smtp.sohu.com");
            prop.setProperty("mail.smtp.auth", "true");
            prop.setProperty("mail.transport.protocol", "SMTP");//邮件配置信息
            Session s=Session.getDefaultInstance(prop,auth);
    
            MimeMessage me=new MimeMessage(s);
            InternetAddress to=new InternetAddress("1278808836@qq.com");
            me.addRecipient(Message.RecipientType.TO, to);
            me.setFrom(new InternetAddress("ppag38615f19c8c8@sohu.com")); // 设置发送者
            //me.setContent("hello world","text/html;utf-8");
            me.setSubject("主题:创建带附件的邮件");
    
            BodyPart bp=new MimeBodyPart();
            bp.setText("带附件的邮件");
            Multipart mp=new MimeMultipart();
            mp.addBodyPart(bp);
    
            BodyPart atachment=new MimeBodyPart();
            String source="C:\Users\佩\Desktop\1.doc";
            DataSource ds=new FileDataSource(source);//创建数据源
            atachment.setDataHandler(new DataHandler(ds));//创建执行文件的指针
            atachment.setFileName(MimeUtility.encodeWord("1.doc"));//设置附件中的文件名
            mp.addBodyPart(atachment);
    
            me.setContent(mp, "text/html;charset=utf-8");
            Transport t=s.getTransport("smtp");
            t.send(me);
    
        }
        public static void main(String[] args) throws MessagingException, IOException {
            new send2().SendMail();
        }
    
    }
    
  • 相关阅读:
    sublime使用及插件
    Unity 查找
    Unity 3D 的四种坐标系
    C#知识点<4>
    C#知识点<3>
    C#知识点<2>
    排序算法
    OOP的三大特性------封装、继承、多态
    C#常用函数
    C++-------------类和对象
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551274.html
Copyright © 2020-2023  润新知