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
- JavaMail
- JavaMail
- javaMail
- JavaMail
- javamail
- javamail
- javamail
- JAVAMAIL
- javamail
- JavaMail
- JAVAMail
- JavaMail
- javaMail
- javamail
- JavaMail
- javaMail
- javamail
- JavaMail
- Android 游戏编程
- [290] Word Pattern
- hdu5733 tetrahedron(计算几何)
- html前端几种加密方式的整理
- 红黑树知识点小结
- javaMail
- Eclipse中使用JUnit进行单元测试
- HDU 5726 GCD [RMQ+二分]
- Swift UIButton 的设置
- 一分钟教你知道乐观锁和悲观锁的区别
- TabView的使用去除上面的空白, tabview点击后去除效果
- 购物车 全选按钮 选中状态 判断
- conversion to dalvik format failed with error 1
- 卷及神经网络 原理介绍