系统发送邮件给用户


public class MailUtil {
    private  static Logger  logger=LoggerFactory.getLogger();
    public  static  void sendOutlook(String subject, String body, String[] to){
        ExchangeService service=new ExchangeService(ExchangeVersion.Exchange2010_sp2);
        try{
            String mailname=LoaderProperties.getString("mail","mailname");//发件人邮箱号
            String mailpwd=LoaderProperties.getString("mail","mailpwd");//邮箱号密码
           String mailserver=LoaderProperties.getString("mail","mailserver");//服务器地址
            ExchangeCredentials credentials=new WebCredentials(mailname,mailpwd);
            service.setCredentials(credentilas);
            service.setUrl(new URL(mailserver));
            EmailMessage msg=new EmailMessage(service);
            msg.setSubject(subject);//邮件的主题
            msg.setBody(MessageBody.getMessageBodyFromText(body));//邮件的内容
            for (String s : to){//收件人
                msg.getToRecipients().add(s);
            }
            msg.send();
        }catch (URLSyntaxException e){
            logger.error("邮件服务器地址错误");
            logger.error(e.toString());
            e.printStackTrace();
        }catch (ServiceLocalException e){
            logger.error("邮件对象生成错误");
            logger.error(e.toString());
            e.printStackTrace();
        }catch (Exception e){
            logger.error("发送邮件错误");
            logger.error(e.toString());
            e.printStackTrace();
        }
    }

 

}

 

依赖:

 
     com.microsoft.ews-java-api
     ews-java-api
     2.0

 


     org.apache,httpConents
     httpcore
     4.4.9

 

 

 


     org.apache,httpConents
     httpclient
     4.5.2

 

 

用户信息:

 

mailname=dyh  用户名
 mailpwd=dyh  密码

 mailserver=https://www.baidu.com  //发送邮件的服务器地址

发邮件时报错请注意:1.检查服务器地址是否正确

                                       2.检查收件人的地址是否正确

                                       3.检查jar包是否冲突(我在写的时候就是出现jar包冲突,开始一直找不到错误)

 

你可能感兴趣的:(Java,发送邮件)