先说发送邮箱用到的三大类:
创建和解析邮件内容的API :Message类是创建和解析邮件的核心API,它的实例对象代表一封电子邮件。
发送邮件的API:Transport类是发送邮件的核心API类,它的实例对象代表实现了某个邮件发送协议的邮件发送对象,例如SMTP协议。
接收邮件的API:Store类是接收邮件的核心API类,它的实例对象代表实现了某个邮件接收协议的邮件接收对象,例如POP3协议。
还有一个提供会话信息的类Session。
简单地邮件发送的过程:
相关的协议说明:见
smtp:http://baike.baidu.com/link?url=b7Wu4usOdwRnTh3LEZgThoDescGdG9KnEnkdQ-yCRbmFwfB70BZK7issKSRo4h7c6iF1Fzd-zGPksVHBeKAyHK
pop3:http://baike.baidu.com/view/5404.htm
相关的代码:
首先要导入支持发送邮件的相关的包。
1 package com.hyq.test; 2 3 import java.util.Properties; 4 5 import javax.mail.Address; 6 import javax.mail.Message; 7 import javax.mail.MessagingException; 8 import javax.mail.Session; 9 import javax.mail.Transport; 10 import javax.mail.internet.InternetAddress; 11 import javax.mail.internet.MimeMessage; 12 13 14 public class DemoTest { 15 public static void main(String[] args) throws MessagingException { 16 Properties pros=new Properties(); 17 //设置用户授权才可以登陆 18 pros.setProperty("mail.smtp.auth", "true"); 19 pros.setProperty("mail.transport.protocol", "smtp"); 20 21 Session session=Session.getInstance(pros); 22 Message msg=new MimeMessage(session); 23 msg.setFrom(new InternetAddress("hepan2013551631@sina.com")); 24 msg.setText("好的,我爱你"); 25 session.setDebug(true) ; 26 27 Transport tran=session.getTransport(); 28 tran.connect("smtp.sina.com", 25, "hepan2013551631", "hepan2013551631"); 29 tran.sendMessage(msg,new Address[]{new InternetAddress("13257499906@163.com")}); 30 tran.close(); 31 } 32 33 34 }
控制台输出信息说明邮件发送成功:
进入自己的163邮箱可见自己刚才发送的邮件
还可以实现添加多附件发送的要用到的类:MimeMultipart,
相应的代码可参考:
1 package cn.hp.mail; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.Properties; 7 8 import javax.activation.DataHandler; 9 import javax.activation.DataSource; 10 import javax.activation.FileDataSource; 11 import javax.mail.MessagingException; 12 import javax.mail.Session; 13 import javax.mail.internet.MimeBodyPart; 14 import javax.mail.internet.MimeMessage; 15 import javax.mail.internet.MimeMultipart; 16 17 public class Demo3 { 18 public static void main(String[] args) throws MessagingException, IOException { 19 Properties pros=new Properties(); 20 Session session=Session.getInstance(pros); 21 MimeMessage msg=new MimeMessage(session); 22 msg.setSubject("哈哈制造"); 23 24 MimeMultipart msgMultipart=new MimeMultipart("mixed"); 25 MimeBodyPart content=new MimeBodyPart(); 26 MimeBodyPart attach1=new MimeBodyPart(); 27 MimeBodyPart attach2=new MimeBodyPart(); 28 msg.setContent(msgMultipart); 29 30 msgMultipart.addBodyPart(attach2); 31 msgMultipart.addBodyPart(attach1); 32 msgMultipart.addBodyPart(content); 33 34 DataSource ds=new FileDataSource("D:\log\1.txt"); 35 DataHandler dh1=new DataHandler(ds); 36 attach1.setDataHandler(dh1); 37 attach1.setFileName("java1.txt");//设置附件的文件名 38 39 DataSource ds2=new FileDataSource("D:\log\1.txt"); 40 DataHandler dh2=new DataHandler(ds2); 41 attach2.setDataHandler(dh2); 42 attach2.setFileName("java2.txt"); 43 44 MimeMultipart bodyMimeMultipart=new MimeMultipart("related"); 45 46 content.setContent(bodyMimeMultipart); 47 48 MimeBodyPart htmlPart=new MimeBodyPart(); 49 MimeBodyPart jifPart=new MimeBodyPart(); 50 bodyMimeMultipart.addBodyPart(htmlPart); 51 bodyMimeMultipart.addBodyPart(jifPart); 52 53 DataSource jifds=new FileDataSource("D:\log\2.txt"); 54 DataHandler jifdh=new DataHandler(jifds); 55 jifPart.setDataHandler(jifdh); 56 57 htmlPart.setDescription("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"); 58 59 msg.saveChanges(); 60 FileOutputStream fileoutStream = new FileOutputStream("D:\图库\2.eml"); 61 62 msg.writeTo(fileoutStream); 63 fileoutStream.close(); 64 } 65 66 }