zoukankan      html  css  js  c++  java
  • java Email发送及中文乱码处理。

    public class mail
    {
    
      private String pop3Server="";
      private String smtpServer="";
    
      private String srcMailAddr="";
      private String user="";
      private String password="";
    
        public mail()
        {
        }
        public boolean getConfig(){
    
          this.pop3Server="pop.163.com";
          this.smtpServer="smtp.163.com";
    
          this.srcMailAddr="cup209@163.com";
          this.user="cup209";
          this.password="******";
    
          return true;
        }
        public boolean checkMailAddr(String mailAddr){
          if(null==mailAddr){
            return false;
          }
          return mailAddr.matches("^[a-z0-9A-Z_]{1,}@[a-z0-9A-Z_]{1,}$");
        }
        public boolean send(String dstMailAddr,String subject,String content){
    
            if(checkMailAddr(dstMailAddr)){
            System.out.print("send mail fail:dst mail addr error");
            return false;
          }
            try
            {
                this.getConfig();
                Properties props = new Properties();
                Session sendMailSession;
                Store store;
                Transport transport;
                props.put("mail.smtp.auth","true");
                props.put("mail.smtp.host", this.smtpServer); //smtp主机名。
                props.put("mail.smtp.user",this.srcMailAddr); //发送方邮件地址。
                props.put("mail.smtp.password",this.password); //邮件密码。
                PopupAuthenticator popA=new PopupAuthenticator();//邮件安全认证。
                PasswordAuthentication pop = popA.performCheck(this.user,this.password); //填写用户名及密码
                sendMailSession = Session.getInstance(props, popA);
                Message newMessage = new MimeMessage(sendMailSession);
                newMessage.setFrom(new InternetAddress(this.srcMailAddr));
                newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(dstMailAddr));  //接收方邮件地址
                newMessage.setSubject(Tool.convertGBK2UTF8(subject));
                newMessage.setSentDate(new Date());
                String mailContent=content;
               try{
                   newMessage.setText(Tool.gbToUtf8(mailContent)); //邮件正文
               }catch(Exception e){}
                //newMessage.setContent(content,"text/html;charset=GBK");
                transport = sendMailSession.getTransport("smtp");
                transport.send(newMessage);
    
    
            }
            catch (MessagingException ex)
            {
                ex.printStackTrace();
                return false;
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            mail sml = new mail();
            sml.send("cup209@163.com","测试邮件","<a href='http://www.163.com'>测试邮件内容</a>");
        }
    
        public class PopupAuthenticator extends Authenticator{
            String username=null;
            String password=null;
            public PopupAuthenticator(){}
            public PasswordAuthentication performCheck(String user,String pass){
                username = user;
                password = pass;
                return getPasswordAuthentication();
            }
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
    
        }
    }
    
  • 相关阅读:
    当你不知道今天星期几,不妨在编辑器写下这段代码
    自定义注解!绝对是程序员装逼的利器!!
    什么是可串行化MVCC
    Jetpack新成员,一篇文章带你玩转Hilt和依赖注入
    连接真机开发安卓(Android)移动app MUI框架 添加购物车等——混合式开发(四)
    从前世今生聊一聊,大厂为啥亲睐时序数据库
    工作五年,面试官说我只会CRUD!竟然只给我10K!
    bootstrap知识总结
    数据处理的两个基本问题05 零基础入门学习汇编语言42
    转移指令的原理02 零基础入门学习汇编语言44
  • 原文地址:https://www.cnblogs.com/jqy518/p/3305810.html
Copyright © 2011-2022 走看看