JAVA邮件开发包括两大方面:
发送与接收邮件:
创建与解析邮件内容:
邮件服务器按通讯协议可以分为两种类型:
SMTP服务器
POP3/IMAP服务器
发送邮件用的是SMTP服务器,接收邮件是通过POP3服务器
发送邮件的最简单的代码:
package com.szy.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 Demo1 { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props); session.setDebug(true); Message msg = new MimeMessage(session); //msg.setContent("你好啊"); msg.setFrom(new InternetAddress("295508987@qq.com")); Transport transport = session.getTransport(); transport.connect("smtp.sina.com",25,"sun29550","woheni1314"); //transport.send(msg,new Address[]{new InternetAddress("sunzhiyan_syspro@163.com")}); transport.sendMessage(msg,new Address[]{new InternetAddress("295508987@qq.com")}); transport.close(); }}