邮件相关协议
- SMTP
- POP
- IMAP
- MIME
SMTP:简单邮件传输协议,用于传送电子邮件。SMTP服务器把消息转发给用作接收消息的SMTP服务器,用户可以通过POP或IMAP协议获取消息。
POP:邮局协议,当前版本为3,所以也称POP3,该协议主要用来接收邮件。
IMAP:接收邮件的更高级协议,当前到第四版,也称IMAP4。
MIME:多用途网际邮件扩充协议。不是邮件传输协议,主要是定义邮件的传输内容。
Java Mail 简单邮件发送实例:
package com.hml.mail; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SimpleMailSender { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true");// 是否需要认证 props.setProperty("mail.transport.protocol", "smtp"); // 邮件发送使用的协议 Session session = Session.getInstance(props); // 获取默认的Seesion session.setDebug(true); // 设置Debug模式,这样在控制台就可以看到程序与邮件服务器的交互信息 Message msg = new MimeMessage(session); // 创建邮件发送的消息 msg.setText("你好吗?"); // 设置邮件发送的内容,此处为文本内容 msg.setFrom(new InternetAddress("hml_vip@sina.com")); // 设置用哪个邮箱账号发送 Transport transport = session.getTransport(); // 创建发送邮件的对象 // 连接邮件服务器 transport.connect("smtp.sina.com", 25, "hml_vip@sina.com", "******"); transport.sendMessage(msg, new Address[]{new InternetAddress("916812579@qq.com")}); // 发送邮件 //transport.send(msg,new Address[]{new InternetAddress("itcast_test@sohu.com")}); transport.close(); } }
发送HTML内容
package com.hml.mail; import java.io.FileInputStream; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SimpleMailSender { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.sina.com"); // 使用Authenticator认证 Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("hml_vip@sina.com","*****"); } } ); session.setDebug(true); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("hml_vip@sina.com")); msg.setSubject("中文主题"); // 设置主题 msg.setRecipients(RecipientType.TO, InternetAddress.parse("916812579@qq.com")); // 设置收件人 msg.setRecipients(RecipientType.CC, InternetAddress.parse("916812579@qq.com")); // 设置抄送人 msg.setRecipients(RecipientType.BCC, InternetAddress.parse("916812579@qq.com")); // 设置密送人 msg.setContent("<span style='color:red'>中文呵呵呵</span>", "text/html;charset=gbk"); Transport.send(msg); } }
带附件的邮件
package com.hml.mail; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; 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 SimpleMailSender { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.sina.com"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("hml_vip@sina.com", "******"); } }); session.setDebug(true); MimeMessage msg = new MimeMessage(session); //创建消息 msg.setFrom(new InternetAddress("hml_vip@sina.com")); msg.setSubject(" "); msg.setReplyTo(new Address[] { new InternetAddress("916812579@qq.com") }); msg.setRecipients(RecipientType.TO, InternetAddress.parse("916812579@qq.com")); MimeMultipart msgMultipart = new MimeMultipart(); // 创建复杂的消息 msg.setContent(msgMultipart); // 添加复杂的消息到消息中 MimeBodyPart attch = new MimeBodyPart(); // 创建附件 msgMultipart.addBodyPart(attch); DataSource ds = new FileDataSource( "Java.txt"); DataHandler dh = new DataHandler(ds); attch.setDataHandler(dh); attch.setFileName(MimeUtility.encodeText("java.txt")); // MimeUtility处理中文乱码 Transport.send(msg); } }