javaMail

来源:互联网 发布:复制淘口令淘宝没反应 编辑:程序博客网 时间:2024/06/05 16:54

知识点: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();    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝店铺低消费人群占比多怎么办 微信只能在应用商城里面打开怎么办 淘宝买了特价商品店家不发货怎么办 红米5a装不下卡怎么办 红米3s流量太慢怎么办 红米3s触屏失灵怎么办 红米note的4g信号差怎么办 电信4g网速慢怎么办红米手机 红米3电信4g信号不好怎么办 红米note3无法连接4g怎么办 红米2a手机开不了机怎么办 红米2a不支持微信运动怎么办 红米2a开不了机怎么办 红米2a突然开不了机怎么办 苹果手机刷机刷到一半没电了怎么办 红米5手机死屏了怎么办 小米2a手机开不了机怎么办 小米2a长时间没用开不了机怎么办 红米手机玩游戏太卡怎么办 红米2a无限重启怎么办 红米3s像素好差怎么办 红米4x后摄像头进水了怎么办 红米手机卡死了又不能拆电池怎么办 红米1s格式化输入法没了怎么办 红米5a锁屏密码怎么办 红米1s手机开不开机怎么办 红米2a忘记解锁密码怎么办 红米手机忘了解锁图案怎么办 账户密码忘记了手机号不用了怎么办 手机电池用完了冲不进去电怎么办 华为手机电池一体的想扣电池怎么办 苹果微信支付显示需要验证码怎么办 天猫极速退货上门取件预约满怎么办 手机锁死了忘记魅族账号密码怎么办 魅蓝e升级系统开不了机了怎么办 京东第三方店铺显示关闭怎么办 派派怎么提现朋友不够怎么办 派派邀请30个好友才能提现怎么办 派派更换手机号后提现时怎么办 京东白条扫码支付被骗怎么办 实体店买的商品一天后降价怎么办