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(邮箱用户名,密码);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值