首页>代码>Java发邮件开发完整源代码>/email/src/com/util/mail/SimpleMailSender.java
001package com.util.mail;  
002   
003import java.util.Date;   
004import java.util.Properties;  
005import javax.mail.Address;   
006import javax.mail.BodyPart;   
007import javax.mail.Message;   
008import javax.mail.MessagingException;   
009import javax.mail.Multipart;   
010import javax.mail.Session;   
011import javax.mail.Transport;   
012import javax.mail.internet.InternetAddress;   
013import javax.mail.internet.MimeBodyPart;   
014import javax.mail.internet.MimeMessage;   
015import javax.mail.internet.MimeMultipart;   
016   
017/** *//**  
018* 简单邮件(不带附件的邮件)发送器  
019 
020*/   
021public class SimpleMailSender  {   
022/** *//**  
023  * 以文本格式发送邮件  
024  * @param mailInfo 待发送的邮件的信息  
025  */   
026    public boolean sendTextMail(MailSenderInfo mailInfo) {   
027      // 判断是否需要身份认证   
028      MyAuthenticator authenticator = null;   
029      Properties pro = mailInfo.getProperties();  
030      if (mailInfo.isValidate()) {   
031      // 如果需要身份认证,则创建一个密码验证器   
032        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());   
033      }  
034      // 根据邮件会话属性和密码验证器构造一个发送邮件的session   
035      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
036      try {   
037      // 根据session创建一个邮件消息   
038      Message mailMessage = new MimeMessage(sendMailSession);   
039      // 创建邮件发送者地址   
040      Address from = new InternetAddress(mailInfo.getFromAddress());   
041      // 设置邮件消息的发送者   
042      mailMessage.setFrom(from);   
043      // 创建邮件的接收者地址,并设置到邮件消息中   
044      Address to = new InternetAddress(mailInfo.getToAddress());   
045      mailMessage.setRecipient(Message.RecipientType.TO,to);   
046      // 设置邮件消息的主题   
047      mailMessage.setSubject(mailInfo.getSubject());   
048      // 设置邮件消息发送的时间   
049      mailMessage.setSentDate(new Date());   
050      // 设置邮件消息的主要内容   
051      String mailContent = mailInfo.getContent();   
052      mailMessage.setText(mailContent);   
053      // 发送邮件   
054      Transport.send(mailMessage);  
055      return true;   
056      } catch (MessagingException ex) {   
057          ex.printStackTrace();   
058      }   
059      return false;   
060    }   
061        
062    /** *//**  
063      * 以HTML格式发送邮件  
064      * @param mailInfo 待发送的邮件信息  
065      */   
066    public static boolean sendHtmlMail(MailSenderInfo mailInfo){   
067      // 判断是否需要身份认证   
068      MyAuthenticator authenticator = null;  
069      Properties pro = mailInfo.getProperties();  
070      //如果需要身份认证,则创建一个密码验证器    
071      if (mailInfo.isValidate()) {   
072        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());  
073      }   
074      // 根据邮件会话属性和密码验证器构造一个发送邮件的session   
075      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
076      try {   
077      // 根据session创建一个邮件消息   
078      Message mailMessage = new MimeMessage(sendMailSession);   
079      // 创建邮件发送者地址   
080      Address from = new InternetAddress(mailInfo.getFromAddress());   
081      // 设置邮件消息的发送者   
082      mailMessage.setFrom(from);   
083      // 创建邮件的接收者地址,并设置到邮件消息中   
084      Address to = new InternetAddress(mailInfo.getToAddress());   
085      // Message.RecipientType.TO属性表示接收者的类型为TO   
086      mailMessage.setRecipient(Message.RecipientType.TO,to);   
087      // 设置邮件消息的主题   
088      mailMessage.setSubject(mailInfo.getSubject());   
089      // 设置邮件消息发送的时间   
090      mailMessage.setSentDate(new Date());   
091      // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象   
092      Multipart mainPart = new MimeMultipart();   
093      // 创建一个包含HTML内容的MimeBodyPart   
094      BodyPart html = new MimeBodyPart();   
095      // 设置HTML内容   
096      html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");   
097      mainPart.addBodyPart(html);   
098      // 将MiniMultipart对象设置为邮件内容   
099      mailMessage.setContent(mainPart);   
100      // 发送邮件   
101      Transport.send(mailMessage);   
102      return true;   
103      } catch (MessagingException ex) {   
104          ex.printStackTrace();   
105      }   
106      return false;   
107    }   
108}  
最近下载更多
1358849392  LV21 2022年11月11日
123465789  LV1 2022年8月5日
dh18262817717  LV3 2022年2月21日
1029162867  LV13 2021年12月25日
zhaojialiang  LV7 2021年5月14日
怎么这么麻烦  LV1 2021年4月8日
15286869652  LV2 2020年12月23日
bibcc  LV2 2020年7月17日
zhangtian1997  LV10 2020年7月13日
sun lover  LV2 2020年6月12日
最近浏览更多
80730176  LV7 2024年6月7日
annazhang  LV29 2023年4月7日
Tuxxxxx  LV3 2023年1月3日
1358849392  LV21 2022年11月11日
crosa_Don  LV18 2022年9月15日
123465789  LV1 2022年8月5日
kahvia  LV1 2022年5月8日
moomin709  LV24 2022年2月28日
羞羞小子  LV13 2022年2月27日
dh18262817717  LV3 2022年2月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友