javamail发送邮件的demo

      今天老大提出一个需求,说如果用例执行错误后就发一个邮件通知给他。看到这个需求后,我就觉得这个比较麻烦,其实是因为我以前没有写过发邮件的程序,所以对未知的东西总有点怕怕的,我这个老大人很好,说你尽量快点实现就可以了,不会强迫我什么时候一定要完成这个功能什么的,这么好的老大很少见了哦。好了废话少说了,看我的发邮件的程序吧,第一:要将程序中需要的mail.jar和activation.jar放到工程的lib目录下,然后引进工程里面去。

                    第二:写程序、

 package com.carryquote.autotest.util.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author suse
 *
 */
public class MailUtil {

    /**
     * @param args
     */
    
    private   String   host= "smtp.163.com";//主机;
    private   String   user= "******@163.com";//用户名;
    private   String   password= "********";//密码;
    private   String   from= "***************@163.com";//发件人;
    private   String   to= "*****@carryquote.com";//收件人;
    private   String   subjetc= "Hahaha";//标题;
    private   String   content= "testteststtst";//内容;

    //设置内容
    public   void   setContent(String   content)   {
    this.content   =   content;
    }
    //设置收件人
    public   void   setTo(String   to)   {
    this.to   =   to;
    }

    /**
      *   发送email
      *   @throws   MessagingException
      *   @throws   Exception
      */
    public   void   sendMail()   throws   MessagingException,Exception{

    Properties   props   =   new   Properties();
            props.put( "mail.smtp.host",   host);//指定SMTP服务器
            props.put( "mail.smtp.auth",   "true");//指定是否需要SMTP验证
       
            Session   mailSession   =   Session.getDefaultInstance(props);
         
            Message   message=new   MimeMessage(mailSession);
            message.setFrom(new   InternetAddress(from));//发件人
            message.addRecipient(Message.RecipientType.TO,new   InternetAddress(to));//收件人
         
            message.setSubject(subjetc);//邮件主题
            message.setText(content);//邮件内容
            message.saveChanges();
         
            Transport   transport   =   mailSession.getTransport( "smtp");
            transport.connect(host,   user,   password);
            transport.sendMessage(message,   message.getAllRecipients());
            transport.close();
           
            }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            new   MailUtil().sendMail();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值