以编程方式发送自动电子邮件[重复]
Posted
技术标签:
【中文标题】以编程方式发送自动电子邮件[重复]【英文标题】:Send auto email programmatically [duplicate] 【发布时间】:2011-06-07 19:40:50 【问题描述】:我想以编程方式发送电子邮件。
我尝试了以下代码。
最终意图 emailIntent = new Intent( android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] "abc@gmail.com" ); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Email Subject"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Email Body"); startActivity(Intent.createChooser( emailIntent, "Send mail..."));
但问题是在发送电子邮件之前应用程序打开活动
我想直接发送电子邮件而无需打开撰写活动。这怎么可能?
【问题讨论】:
你为什么不试试 startActivityforResult 的东西? 这能回答你的问题吗? Sending Email in Android using JavaMail API without using the default/built-in app 【参考方案1】:使用 Kotlin 以编程方式发送电子邮件。
简单的电子邮件发送,而不是所有其他功能(如附件)。 TLS 始终开启 也只需要 1 个 gradle 电子邮件依赖项。我还发现这个电子邮件 POP 服务列表非常有用:
https://support.office.com/en-gb/article/pop-and-imap-email-settings-for-outlook-8361e398-8af4-4e97-b147-6c6c4ac95353
使用方法:
val auth = EmailService.UserPassAuthenticator("yourUser", "yourPass")
val to = listOf(InternetAddress("to@email.com"))
val from = InternetAddress("from@email.com")
val email = EmailService.Email(auth, to, from, "Test Subject", "Hello Body World")
val emailService = EmailService("yourSmtpServer", 587)
GlobalScope.launch // or however you do background threads
emailService.send(email)
代码:
import java.util.*
import javax.mail.*
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
class EmailService(private var server: String, private var port: Int)
data class Email(
val auth: Authenticator,
val toList: List<InternetAddress>,
val from: Address,
val subject: String,
val body: String
)
class UserPassAuthenticator(private val username: String, private val password: String) : Authenticator()
override fun getPasswordAuthentication(): PasswordAuthentication
return PasswordAuthentication(username, password)
fun send(email: Email)
val props = Properties()
props["mail.smtp.auth"] = "true"
props["mail.user"] = email.from
props["mail.smtp.host"] = server
props["mail.smtp.port"] = port
props["mail.smtp.starttls.enable"] = "true"
props["mail.smtp.ssl.trust"] = server
props["mail.mime.charset"] = "UTF-8"
val msg: Message = MimeMessage(Session.getDefaultInstance(props, email.auth))
msg.setFrom(email.from)
msg.sentDate = Calendar.getInstance().time
msg.setRecipients(Message.RecipientType.TO, email.toList.toTypedArray())
// msg.setRecipients(Message.RecipientType.CC, email.ccList.toTypedArray())
// msg.setRecipients(Message.RecipientType.BCC, email.bccList.toTypedArray())
msg.replyTo = arrayOf(email.from)
msg.addHeader("X-Mailer", CLIENT_NAME)
msg.addHeader("Precedence", "bulk")
msg.subject = email.subject
msg.setContent(MimeMultipart().apply
addBodyPart(MimeBodyPart().apply
setText(email.body, "iso-8859-1")
//setContent(email.htmlBody, "text/html; charset=UTF-8")
)
)
Transport.send(msg)
companion object
const val CLIENT_NAME = "Android *** programmatic email"
分级:
dependencies
implementation 'com.sun.mail:android-mail:1.6.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
AndroidManifest:
<uses-permission name="android.permission.INTERNET" />
【讨论】:
【参考方案2】:这可能是最简单的方法-
String recipientList = mEditTextTo.getText().toString();
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
【讨论】:
【参考方案3】:引用的链接有正确答案,但有编写一些库来让您的工作轻松。
https://github.com/yesidlazaro/GmailBackground https://github.com/thegenuinegourav/Android-Email-App-using-Javamail-Api https://github.com/prashantwosti/BackgroundMailLibrary所以不要重新编写所有代码,只需使用这些库中的任何一个即可在短时间内完成您的工作。
【讨论】:
以上是关于以编程方式发送自动电子邮件[重复]的主要内容,如果未能解决你的问题,请参考以下文章
使用 Excel VBA 以编程方式禁止 Outlook 电子邮件发送警告消息