You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓开发:如何使用企业邮箱而非Gmail实现后台发送邮件功能

安卓企业邮箱后台发送实现方案

1 实现逻辑说明

你现有两套方案中,Intent唤起系统邮件选择器的方案需要用户手动操作,BackgroundMail是Gmail专属适配工具类,没有自定义SMTP配置入口,无法直接复用。企业邮箱后台发送需要基于通用SMTP协议实现,使用Android适配版JavaMail库即可完成。

2 第一步:添加依赖

在app模块的build.gradle的dependencies代码块中添加如下依赖:

implementation 'com.sun.mail:android-mail:1.6.7'
implementation 'com.sun.mail:android-activation:1.6.7'

3 第二步:自定义企业邮件发送工具类

import android.os.AsyncTask;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class EnterpriseMailSender extends AsyncTask<Void, Void, Boolean> {
    // 以下企业邮箱SMTP参数请先找你司邮箱管理员确认正确值
    private static final String SMTP_HOST = "smtp.wind.energy";
    private static final String SMTP_PORT = "465";
    private static final boolean USE_SSL = true;

    private final String senderEmail = "m.nebo@wind.energy";
    private final String senderPassword = "你的企业邮箱密码/授权码";
    private final String receiverEmail;
    private final String subject;
    private final String content;
    private final String attachmentPath; // 无附件传null即可

    public EnterpriseMailSender(String receiverEmail, String subject, String content, String attachmentPath) {
        this.receiverEmail = receiverEmail;
        this.subject = subject;
        this.content = content;
        this.attachmentPath = attachmentPath;
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.auth", "true");
        if(USE_SSL){
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
        } else {
            props.put("mail.smtp.starttls.enable", "true");
        }

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderEmail, senderPassword);
            }
        });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(senderEmail));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
            message.setSubject(subject);

            // 组装正文和附件
            MimeMultipart multipart = new MimeMultipart();
            // 正文部分
            MimeBodyPart textPart = new MimeBodyPart();
            textPart.setText(content);
            multipart.addBodyPart(textPart);

            // 附件部分
            if(attachmentPath != null && !attachmentPath.isEmpty()){
                MimeBodyPart attachmentPart = new MimeBodyPart();
                FileDataSource source = new FileDataSource(attachmentPath);
                attachmentPart.setDataHandler(new DataHandler(source));
                attachmentPart.setFileName(source.getName());
                multipart.addBodyPart(attachmentPart);
            }

            message.setContent(multipart);
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean success) {
        super.onPostExecute(success);
        // 此处可自定义发送结果回调逻辑,比如成功提示、失败重试
    }
}

4 第三步:调用示例

和你原来BackgroundMail的调用逻辑一致,直接在需要发邮件的地方调用即可:

// 带附件发送示例
String pdfPath = filePath.exists() ? Pdf_Path : null;
new EnterpriseMailSender(Commen.Email_Abrechnung, emailBetreff, emailText, pdfPath).execute();

注意事项

  • 先在AndroidManifest.xml中添加必要权限:
<uses-permission android:name="android.permission.INTERNET" />
<!-- 读取附件需要的存储权限,根据你的targetSdk版本适配 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
  • 多数企业邮箱SMTP发信需要使用授权码而非账号登录密码,需要提前确认
  • 不要把邮箱密码硬编码在客户端代码中,容易被反编译泄露,生产环境建议把发信逻辑放到后端服务,客户端仅调用后端发信接口
  • 安卓10及以上版本需要适配分区存储规则,确保附件路径可被正常读取

内容的提问来源于stack exchange,提问作者Nebo Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 05:06:08