android后台发送邮件详解编程语言

public class SendEmail { 
    private static final String TAG = "SendEmail"; 
    //要发送Email地址 
    private String mailTo = null; 
    //邮件发送来源地址 
    private String mailFrom = null; 
    //SMTP主机地址 
    private String smtpHost = null; 
    //是否启用调试 
    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内容 
    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; 
 
    /* 
    默认构造函数 
     */ 
    public SendEmail() { 
        super(); 
    } 
 
    private void writeEmail(Session session, Message message) throws MessagingException { 
        String fileName; 
        Multipart multipart = new MimeMultipart(); 
        //设定发件人地址 
        if (mailFrom != null) { 
            message.setFrom(new InternetAddress(mailFrom)); 
            Log.i(TAG, "发件人邮件地址:" + mailFrom); 
        } else { 
            Log.i(TAG, "没有指定发件人邮件地址"); 
            return; 
        } 
        //设定收件人地址 
        if (mailTo != null) { 
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); 
            Log.i(TAG, "收件人邮件地址:" + mailTo); 
        } else { 
            Log.i(TAG, "没有指定收件人邮件地址"); 
            return; 
        } 
        //设定抄送地址 
        if (emailccTo != null) { 
            message.setRecipient(Message.RecipientType.CC, new InternetAddress(emailccTo)); 
            Log.i(TAG, "抄送邮件地址:" + emailccTo); 
        } else { 
            Log.i(TAG, "没有指定抄送邮件地址"); 
            return; 
        } 
        //设定密送地址 
        if (emailbccTo != null) { 
            message.setRecipient(Message.RecipientType.BCC, new InternetAddress(emailbccTo)); 
            Log.i(TAG, "密送邮件地址:" + emailbccTo); 
        } else { 
            Log.i(TAG, "没有指定密送邮件地址"); 
            return; 
        } 
        //设置邮件主题 
        message.setSubject(subject); 
        Log.i(TAG, "邮件主题:" + subject); 
        //设置回复地址 
        message.setReplyTo(new InternetAddress[]{new InternetAddress(mailFrom)}); 
        //创建并设置第一部分 
        MimeBodyPart bodyPart = new MimeBodyPart(); 
        if (msgContent != null) { 
            Log.i(TAG, "邮件内容:" + 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发送的附件为:" + messageBasePath + fileName); 
                mBodyPart.setDataHandler(new DataHandler(fds)); 
                mBodyPart.setFileName(fileName); 
                multipart.addBodyPart(mBodyPart); 
            } 
        } 
        Log.i(TAG, "设置邮件部分"); 
        message.setContent(multipart); 
        message.setSentDate(new Date()); 
    } 
 
    /** 
     * 发送邮件方法 
     * 
     * @return true 表示发送成功,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的话会出现错误 
        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, "开始连接服务器"); 
                transport.connect(smtpHost, 25, MailAuthenticator.TENCENT_EMAIL_USER, MailAuthenticator.TENCENT_EMAIL_PWD); 
            } catch (AuthenticationFailedException e) { 
                e.printStackTrace(); 
                Log.i(TAG, "连接服务器失败"); 
                return false; 
            } catch (MessagingException e) { 
                e.printStackTrace(); 
                Log.i(TAG, "发送邮件过程中出现错误"); 
                return false; 
            } 
            Log.i(TAG, "开始发送邮件"); 
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); 
            transport.close(); 
            Log.i(TAG, "关闭连接"); 
        } catch (MessagingException e) { 
            e.printStackTrace(); 
            Log.i(TAG, "发送邮件失败"); 
            return false; 
        } finally { 
            try { 
                if (transport != null && transport.isConnected()) { 
                    transport.close(); 
                    Log.i(TAG, "在finally中关闭连接"); 
                } 
            } catch (MessagingException e) { 
                e.printStackTrace(); 
            } 
        } 
        Log.i(TAG, "邮件发送成功"); 
        return true; 
    } 
} 
 
tips---其中的MyAuthenticator类继承自Authenticator,重写这个方法即可 
 
@Override 
protected PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(邮箱用户名,密码); 
}

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10715.html

(0)
上一篇 2021年7月19日 10:23
下一篇 2021年7月19日 10:23

相关推荐

发表回复

登录后才能评论