中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android后臺發(fā)送郵件

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
public class SendEmail {
    private static final String TAG = "SendEmail";
    //要發(fā)送Email地址
    private String mailTo = null;
    //郵件發(fā)送來源地址
    private String mailFrom = null;
    //SMTP主機地址
    private String smtpHost = null;
    //是否啟用調(diào)試
    private boolean debug = false;

    private String messageBasePath = null;
    //Email主題
    private String subject;

    public void setMailTo(String mailTo) {
        this.mailTo = mailTo;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public void setMessageBasePath(String messageBasePath) {
        this.messageBasePath = messageBasePath;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setMsgContent(String msgContent) {
        this.msgContent = msgContent;
    }

    public void setAttachedFileList(Vector attachedFileList) {
        this.attachedFileList = attachedFileList;
    }

    public void setEmailAccount(String emailAccount) {
        this.emailAccount = emailAccount;
    }

    public void setEmailPwd(String emailPwd) {
        this.emailPwd = emailPwd;
    }

    public void setMessageContentType(String messageContentType) {
        this.messageContentType = messageContentType;
    }

    public void setEmailbccTo(String emailbccTo) {
        this.emailbccTo = emailbccTo;
    }

    public void setEmailccTo(String emailccTo) {
        this.emailccTo = emailccTo;
    }

    //Email內(nèi)容
    private String msgContent;

    private Vector attachedFileList;
    private String emailAccount = null;
    private String emailPwd = null;
    private String messageContentType = "text/html;charset=utf-8";

    private String emailbccTo = null;
    private String emailccTo = null;

    /*
    默認(rèn)構(gòu)造函數(shù)
     */
    public SendEmail() {
        super();
    }

    private void writeEmail(Session session, Message message) throws MessagingException {
        String fileName;
        Multipart multipart = new MimeMultipart();
        //設(shè)定發(fā)件人地址
        if (mailFrom != null) {
            message.setFrom(new InternetAddress(mailFrom));
            Log.i(TAG, "發(fā)件人郵件地址:" + mailFrom);
        } else {
            Log.i(TAG, "沒有指定發(fā)件人郵件地址");
            return;
        }
        //設(shè)定收件人地址
        if (mailTo != null) {
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
            Log.i(TAG, "收件人郵件地址:" + mailTo);
        } else {
            Log.i(TAG, "沒有指定收件人郵件地址");
            return;
        }
        //設(shè)定抄送地址
        if (emailccTo != null) {
            message.setRecipient(Message.RecipientType.CC, new InternetAddress(emailccTo));
            Log.i(TAG, "抄送郵件地址:" + emailccTo);
        } else {
            Log.i(TAG, "沒有指定抄送郵件地址");
            return;
        }
        //設(shè)定密送地址
        if (emailbccTo != null) {
            message.setRecipient(Message.RecipientType.BCC, new InternetAddress(emailbccTo));
            Log.i(TAG, "密送郵件地址:" + emailbccTo);
        } else {
            Log.i(TAG, "沒有指定密送郵件地址");
            return;
        }
        //設(shè)置郵件主題
        message.setSubject(subject);
        Log.i(TAG, "郵件主題:" + subject);
        //設(shè)置回復(fù)地址
        message.setReplyTo(new InternetAddress[]{new InternetAddress(mailFrom)});
        //創(chuàng)建并設(shè)置第一部分
        MimeBodyPart bodyPart = new MimeBodyPart();
        if (msgContent != null) {
            Log.i(TAG, "郵件內(nèi)容:" + msgContent);
            bodyPart.setContent(msgContent, messageContentType);
        } else {
            bodyPart.setContent("", messageContentType);
        }
        multipart.addBodyPart(bodyPart);
        //附件文件到郵件中
        if (attachedFileList != null) {
            for (Enumeration fileList = attachedFileList.elements(); fileList.hasMoreElements(); ) {
                fileName = (String) fileList.nextElement();
                MimeBodyPart mBodyPart = new MimeBodyPart();

                FileDataSource fds = new FileDataSource(messageBasePath + fileName);
                Log.i(TAG, "Email發(fā)送的附件為:" + messageBasePath + fileName);
                mBodyPart.setDataHandler(new DataHandler(fds));
                mBodyPart.setFileName(fileName);
                multipart.addBodyPart(mBodyPart);
            }
        }
        Log.i(TAG, "設(shè)置郵件部分");
        message.setContent(multipart);
        message.setSentDate(new Date());
    }

    /**
     * 發(fā)送郵件方法
     *
     * @return true 表示發(fā)送成功,false表示不成功
     */
    public boolean sendEmail() {
        int loopCount;
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", smtpHost);
        properties.setProperty("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "25");
        MailAuthenticator authenticator = new MailAuthenticator();
        Session session = Session.getInstance(properties, authenticator);
        session.setDebug(debug);
        MimeMessage mimeMessage = new MimeMessage(session);

//這里如果用Transport的話會出現(xiàn)錯誤
        SMTPTransport transport = new SMTPTransport(session, new URLName("smtp", "smtp.qq.com", 25, null, MailAuthenticator.TENCENT_EMAIL_USER, MailAuthenticator.TENCENT_EMAIL_PWD));
        try {
            writeEmail(session, mimeMessage);
            //transport = session.getTransport("smtp");
            try {
                Log.i(TAG, "開始連接服務(wù)器");
                transport.connect(smtpHost, 25, MailAuthenticator.TENCENT_EMAIL_USER, MailAuthenticator.TENCENT_EMAIL_PWD);
            } catch (AuthenticationFailedException e) {
                e.printStackTrace();
                Log.i(TAG, "連接服務(wù)器失敗");
                return false;
            } catch (MessagingException e) {
                e.printStackTrace();
                Log.i(TAG, "發(fā)送郵件過程中出現(xiàn)錯誤");
                return false;
            }
            Log.i(TAG, "開始發(fā)送郵件");
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();
            Log.i(TAG, "關(guān)閉連接");
        } catch (MessagingException e) {
            e.printStackTrace();
            Log.i(TAG, "發(fā)送郵件失敗");
            return false;
        } finally {
            try {
                if (transport != null && transport.isConnected()) {
                    transport.close();
                    Log.i(TAG, "在finally中關(guān)閉連接");
                }
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        Log.i(TAG, "郵件發(fā)送成功");
        return true;
    }
}

tips---其中的MyAuthenticator類繼承自Authenticator,重寫這個方法即可

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(郵箱用戶名,密碼);
}

標(biāo)簽: 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:fmdb常用操作代碼

下一篇:C#創(chuàng)建windows系統(tǒng)用戶