使用javaMail发送邮件

由于项目需要使用发送邮件功能,因此去网上找了下相关资料,发现都是使用JavaMail来实现此功能,因此便也决定使用其来开发。下面为具体实现

                                                                                                                                                                                          -------------------------------------------写的不好轻喷。。。。

第一步:下载JavaMail.jar包和jaf.jar包

现在最新版的jar包为1.5.4 下载地址为:https://java.net/projects/javamail/pages/Home

另外需下载jaf.jar,其最新版本为1.1下载地址为: http://www.oracle.com/technetwork/java/jaf11-139815.html


第二步:创建email.properties

该properties文件内容为:

#邮箱所属SMTP服务器
host=smtp.qq.com
#发送人邮箱   该邮箱必须开通POP/SMTP服务  具体开通方法请找度娘
email=664980432@qq.com
#发送人SMTP账户     账户名就是把上面的邮箱地址@qq.com去掉
user=664980432
#发送人SMTP账户独立密码   开通POP/SMTP服务后会要求你设置个独立密码
password=aaaaaa


第三步:解析email.properties文件

public Properties loadProperties() {
  Properties props = new Properties();
   InputStream is = PropertiesLoader.class.getClassLoader().getResourceAsStream
   ("email.properties");  //注意properties路径
   try {
    //Resource resource = resourceLoader.getResource(resourcesPaths);
    //is = resource.getInputStream();
    props.load(is);
   } catch (IOException ex) {
    ex.printStackTrace();
   } finally {
    IOUtils.closeQuietly(is);
   }
  return props;
 }


第四步:发送邮件

package light.mvc.utils;

import java.util.Date;  
import java.util.Properties; 
import javax.mail.Address;  
import javax.mail.Authenticator;
import javax.mail.BodyPart;  
import javax.mail.Message;  
import javax.mail.MessagingException;  
import javax.mail.Multipart;  
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;  
 
/** 
* 简单邮件(不带附件的邮件)发送器 
*/  
public class MailSender  {
  static Properties pro = emailConfig();
 public static Properties emailConfig(){
    Properties props = new Properties();
   InputStream is = PropertiesLoader.class.getClassLoader().getResourceAsStream
   ("email.properties");
   try {
    //Resource resource = resourceLoader.getResource(resourcesPaths);
    //is = resource.getInputStream();
    props.load(is);
   } catch (IOException ex) {
    ex.printStackTrace();
   } finally {
    IOUtils.closeQuietly(is);
   }
  return props;
 }
/** 
  * 以文本格式发送邮件 
  * @param mailInfo 待发送的邮件的信息 
  */  
    public boolean sendTextMail(String toEmail,String title,String text) {  
     pro = emailConfig();

      //用户名、密码验证
      Authenticator authenticator = new Authenticator() {           
       @Override           
       protected PasswordAuthentication getPasswordAuthentication() {
        // 用户名、密码              
        String userName = pro.getProperty("user");               
        String password = pro.getProperty("password");
        return new PasswordAuthentication(userName, password);           
        }       
       };   
    Properties property = new Properties(); 
     property.setProperty("mail.transport.protocol", "smtp"); 
     property.setProperty("mail.smtp.auth", "true"); 
      // 根据邮件会话属性和密码验证器构造一个发送邮件的session  
      Session sendMailSession = Session.getInstance(property, authenticator);  
      sendMailSession.setDebug(true);

    
      try {  
       // 由 Session 对象获得 Transport 对象 
       Transport transport = sendMailSession.getTransport(); 
       // 发送用户名、密码连接到指定的 smtp 服务器 
       transport.connect(pro.getProperty("host"), pro.getProperty("user"), pro.getProperty("password")); 
      // 根据session创建一个邮件消息  
      Message mailMessage = new MimeMessage(sendMailSession);  
      // 创建邮件发送者地址  
      Address from = new InternetAddress(pro.getProperty("email"));  
      // 设置邮件消息的发送者  
      mailMessage.setFrom(from);  
      // 创建邮件的接收者地址,并设置到邮件消息中  
      Address to = new InternetAddress(toEmail);  
      mailMessage.setRecipient(Message.RecipientType.TO,to);  
      // 设置邮件消息的主题  
      mailMessage.setSubject(title);  
      // 设置邮件消息发送的时间  
      mailMessage.setSentDate(new Date());  
      // 设置邮件消息的主要内容  
      String mailContent = text;  
      mailMessage.setText(mailContent);  
      // 发送邮件  
      transport.sendMessage(mailMessage, mailMessage.getRecipients(Message.RecipientType.TO)); 
      transport.close();
      return true;  
      } catch (MessagingException ex) {  
          ex.printStackTrace();  
      }  
      return false;  
    }   
      
      
  
}


----------------------------------------------------------分割线------------------------------------------------------------

以上就为使用JavaMail发送邮件的具体实现。如有不同意见可以讨论讨论


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值