邮件发送

来源:互联网 发布:foxit editor mac 编辑:程序博客网 时间:2024/06/12 00:43

邮件开发的相关协议:

SMTP:Simple Message Transfer Protocal发送协议 默认端口:25
POP:Post Office Protocal邮局协议。POP3这个版本用的最多,接收协议 默认端口:110


邮件发送和接收过程分析:




一:准备工作:申请邮箱

我在这里申请新浪、网易邮箱,进入后开通POP3/SMTP服务



二:导入依赖

<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.4</version></dependency><!--spring继承javaMail--><dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context-support</artifactId>      <version>${spring.version}</version></dependency>


如果是web项目,导入jar包



三:代码实现

import java.util.Properties;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType; public class MailDemo01 { public static void main(String[]args) throws Exception {//1 设置发送电子邮件的参数Properties props = new Properties();//设置邮件服务器 smtp.qq.com   smtp.sina.com  smtp.163.com  smtp.aliyun.comprops.put("mail.smtp.host","smtp.163.com");//验证是否打开props.put("mail.smtp.auth","true");//2 取的与邮件服务器的连接Session session = Session.getInstance(props);//3 创建一封邮件MimeMessage mimeMessage = new MimeMessage(session);//4 设置发送者InternetAddress address = new InternetAddress("shitcast@163.com");mimeMessage.setFrom(address);//5 设置接收者InternetAddress toAddress = new InternetAddress("1638064027@qq.com");//第一个参数:to:直接接收者人   ;cc 抄送;bcc:暗送mimeMessage.setRecipient(RecipientType.TO,toAddress);//6 设置邮件标题、内容mimeMessage.setSubject("约吗?");mimeMessage.setText("这是一封来自百合网的有约信。亲爱的,晚上有空吗?操场见");mimeMessage.saveChanges();//7 发送:坐火箭Transport transport = session.getTransport("smtp");//登录邮件transport.connect("smtp.163.com","shitcast@163.com", "q7w8e9a4s5d6");//第一个参数:邮件内容///第二个参数:所有的接收者transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//关闭火箭transport.close();System.out.println("发送成功");}}


四:将上述代码封装成工具类,fnagbian


public class MailUtil {/** * @param toAddress接收者 * @param subject主题 * @param text邮件内容 * @throws Exception */public static void sendMail(StringtoAddress,String subject,String text) throws Exception{//1 设置邮件接收的参数Properties props =new Properties();props.put("mail.smtp.host","smtp.163.com");props.put("mail.smtp.auth","true");//2 获取连接Session session = Session.getInstance(props);//3 创建邮件MimeMessage mimeMessage =new MimeMessage(session);//4 设置发送者InternetAddress address =new InternetAddress("shitcast@163.com");mimeMessage.setFrom(address);//5 设置接收者InternetAddress toAddress2 =new InternetAddress(toAddress);mimeMessage.setRecipient(RecipientType.TO,toAddress2);//6 设置邮件内容mimeMessage.setSubject(subject);mimeMessage.setText(text);mimeMessage.saveChanges();//7 坐火箭Transport transport =session.getTransport("smtp");transport.connect("smtp.163.com","shitcast@163.com", "q7w8e9a4s5d6");transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());transport.close();}//测试一波public static void main(String[]args) {try {MailUtil.sendMail("1638064027@qq.com","来吧?","一起学习");} catch (Exceptione) {// TODO Auto-generated catch blocke.printStackTrace();}}}



结束!!!试试吧