java發送郵件

來源:互聯網
上載者:User

標籤:vax   style   htm   top   smtp   imp   turn   stat   protocol   

import java.util.Properties;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.mail.Address;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SendEmail {    /**     *

      

String uto="[email protected]";//擷取收件者郵箱
String topic="系統報錯";//發送主題
String content="error";//發送內容
String sendemail="[email protected]";
String sendname="終結者系統";
String connecthost="smtp.163.com";
String connectemailname="sys857";
String connectemailpass="cyunlnvuctkqwtvs";

     * @param uto 擷取收件者郵箱     * @param topic 發送主題     * @param content 發送內容     * @param sendemail 發送人的郵箱      * @param sendname 發送人姓名        * @param connecthost 郵箱主機      * @param connectemailname 信箱使用者名       * @param connectemailpass  郵箱發送密鑰(不是登入密碼)     * @return boolean      */        public static boolean sendemail(String uto,String topic,String content,            String sendemail,String sendname,String connecthost,            String connectemailname,String connectemailpass,Integer port)        {                        //驗證郵箱格式開始        boolean flag=true;                String str="^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";        Pattern regex=Pattern.compile(str);        Matcher matcher=regex.matcher(uto);        flag=matcher.matches();                //郵箱驗證                try {        if(flag){        //設定協議        Properties pros = new Properties();        pros.put("mail.transport.protocol","smtp");        pros.put("mail.smtp.auth", true);        pros.put("mail.smtp.port", port);  //   qq         pros.setProperty("mail.debug", "false");        //Authenticator authenticator=new Authenticator();        Session session=Session.getInstance(pros);        session.setDebug(true);                        //設定發送的郵件                MimeMessage msg = new MimeMessage(session);        //設定發送人的郵箱和姓名         Address toaddress=new InternetAddress(sendemail,sendname);        msg.setFrom(toaddress);            //        /*                Address[] cc={new InternetAddress(""),new InternetAddress("")};        msg.setRecipients(Message.RecipientType.CC, cc);        */            msg.setRecipients(Message.RecipientType.TO, uto);//設定收件者            msg.setSubject(topic);//主題            msg.setText(content);//內容            msg.setContent(content,"text/html;charset=utf-8");//發送html格式的郵件            msg.saveChanges();//儲存發送                        //取得發送工具            Transport ts=session.getTransport();            //設定串連 郵箱主機  信箱使用者名 郵箱密碼            ts.connect(connecthost,connectemailname,connectemailpass);            ts.sendMessage(msg, msg.getAllRecipients());//發送            ts.close();//關閉                        return true;        }else{            return false;        }        }catch(Exception e){            return false;        }    }    }

 

java發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.