如何在Android应用中使用Java通过SendGrid和Amazon Work Email发送邮件
在Android应用中用Java实现SendGrid与Amazon Work Email发送邮件
一、使用SendGrid发送邮件
步骤1:添加依赖与权限
在Module级别的build.gradle中引入SendGrid官方依赖:
dependencies { implementation 'com.sendgrid:sendgrid-java:4.9.3' }
同时在AndroidManifest.xml中声明网络权限:
<uses-permission android:name="android.permission.INTERNET" />
步骤2:编写发送逻辑(后台线程执行)
Android禁止主线程执行网络操作,这里使用Coroutine切换到IO线程实现:
import com.sendgrid.Method; import com.sendgrid.Request; import com.sendgrid.Response; import com.sendgrid.SendGrid; import com.sendgrid.helpers.mail.Mail; import com.sendgrid.helpers.mail.objects.Content; import com.sendgrid.helpers.mail.objects.Email; import kotlinx.coroutines.Dispatchers; import kotlinx.coroutines.GlobalScope; import kotlinx.coroutines.launch; public class SendGridEmailSender { // 替换为你的SendGrid API密钥 private static final String SENDGRID_API_KEY = "your_sendgrid_api_key"; public static void sendTestEmail() { GlobalScope.launch(Dispatchers.IO) { Email from = new Email("your-verified-email@example.com"); String subject = "SendGrid测试邮件"; Email to = new Email("recipient@example.com"); Content content = new Content("text/plain", "这是通过SendGrid发送的测试内容"); Mail mail = new Mail(from, subject, to, content); SendGrid sg = new SendGrid(SENDGRID_API_KEY); Request request = new Request(); try { request.setMethod(Method.POST); request.setEndpoint("mail/send"); request.setBody(mail.build()); Response response = sg.api(request); // 可根据响应状态码处理结果 System.out.println("响应状态码:" + response.getStatusCode()); } catch (Exception ex) { ex.printStackTrace(); } } } }
- 注意:发件邮箱需在SendGrid控制台完成验证,避免邮件被拦截
- 敏感信息(如API密钥)不要硬编码,建议用
EncryptedSharedPreferences存储
二、使用Amazon Work Email发送邮件
Amazon Work Email提供SMTP服务,可通过JavaMail API实现发送。
步骤1:添加依赖
在build.gradle中引入JavaMail相关依赖:
dependencies { implementation 'com.sun.mail:android-mail:1.6.7' implementation 'com.sun.mail:android-activation:1.6.7' }
同样需要声明INTERNET权限。
步骤2:获取SMTP配置信息
在AWS控制台的Amazon Work Email中,找到你的邮箱账号,获取:
- SMTP服务器地址
- 端口(通常为587,启用TLS加密)
- SMTP用户名(你的工作邮箱地址)
- SMTP密码(创建邮箱时设置或重置的密码)
步骤3:编写SMTP发送逻辑
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.MimeMessage; import kotlinx.coroutines.Dispatchers; import kotlinx.coroutines.GlobalScope; import kotlinx.coroutines.launch; public class AwsWorkEmailSender { // 替换为你的SMTP配置 private static final String SMTP_HOST = "your-smtp-server.amazonaws.com"; private static final int SMTP_PORT = 587; private static final String USERNAME = "your-work-email@your-domain.com"; private static final String PASSWORD = "your-smtp-password"; public static void sendTestEmail() { GlobalScope.launch(Dispatchers.IO) { Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME, PASSWORD); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(USERNAME)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Amazon Work Email测试邮件"); message.setText("这是通过Amazon Work Email发送的测试内容"); Transport.send(message); System.out.println("邮件发送成功"); } catch (MessagingException ex) { ex.printStackTrace(); } } } }
通用注意事项
- Android 8.0+对后台网络操作有限制,若需定时/延迟发送,建议使用
WorkManager替代Coroutine - 确保发件邮箱完成域名或地址验证,提升邮件送达率
- 敏感信息必须加密存储,禁止硬编码在代码中
内容的提问来源于stack exchange,提问作者Shehzad Raheem
相关产品推荐
相关产品推荐

