我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
当前回答
其他答案中提供的所有代码都是正确的,运行正常,但有点混乱,所以我决定发布一个库(尽管仍在开发中),以更简单的方式使用它:AndroidMail。
你只需要创建一个MailSender,构建一个邮件并发送它(已经在后台处理了一个AsyncTask)。
MailSender mailSender = new MailSender(email, password);
Mail.MailBuilder builder = new Mail.MailBuilder();
Mail mail = builder
.setSender(senderMail)
.addRecipient(new Recipient(recipient))
.setText("Hello")
.build();
mailSender.sendMail(mail);
您可以收到通知的电子邮件发送,它还支持不同的收件人类型(TO,抄送和密件抄送),附件和html:
MailSender mailSender = new MailSender(email, password);
Mail.MailBuilder builder = new Mail.MailBuilder();
Mail mail = builder
.setSender(senderMail)
.addRecipient(new Recipient(recipient))
.addRecipient(new Recipient(Recipient.TYPE.CC, recipientCC))
.setText("Hello")
.setHtml("<h1 style=\"color:red;\">Hello</h1>")
.addAttachment(new Attachment(filePath, fileName))
.build();
mailSender.sendMail(mail, new MailSender.OnMailSentListener() {
@Override
public void onSuccess() {
// mail sent!
}
@Override
public void onError(Exception error) {
// something bad happened :(
}
});
您可以通过Gradle或Maven获取:
compile 'it.enricocandino:androidmail:1.0.0-SNAPSHOT'
如果你有任何问题,请告诉我!:)
其他回答
你考虑过使用Apache Commons Net吗?从3.3开始,只需要一个jar(您可以使用gradle或maven依赖它),就完成了:http://blog.dahanne.net/2013/06/17/sending-a-mail-in-java-and-android-with-apache-commons-net/
用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("you@gmail.com", "yourPassword")
val to = listOf(InternetAddress("to@email.com"))
val from = InternetAddress("you@gmail.com")
val email = EmailService.Email(auth, to, from, "Test Subject", "Hello Body World")
val emailService = EmailService("smtp.gmail.com", 465)
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 val server: String, private val 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 StackOverflow programmatic email"
}
}
Gradle:
dependencies {
implementation 'com.sun.mail:android-mail:1.6.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
}
安卓清单:
<uses-permission android:name="android.permission.INTERNET" />
package io.formics.tourguide
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Credentials
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_feedback.*
import org.jetbrains.annotations.Async
import java.lang.Exception
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource
import javax.mail.*
import javax.mail.internet.*
class FeedbackActivity : AppCompatActivity() {
val props = Properties()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_feedback)
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
btnSendEmail.setOnClickListener {
Thread {
try {
sendEmail()
// Your implementation
} catch (ex: Exception) {
ex.printStackTrace()
}
}.start()
}
}
private fun sendEmail() {
try {
val session = Session.getInstance(props,
object : javax.mail.Authenticator() {
//Authenticating the password
override fun getPasswordAuthentication(): javax.mail.PasswordAuthentication {
return PasswordAuthentication("abc@xyz.com", "password")
}
})
val message = MimeMessage(session);
message.setFrom(InternetAddress("abc@xyz.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(editCC.text.toString())
)
message.subject = editSubject.text.toString()
message.setText(
"Dear Mail Crawler,"
+ "\n\n No spam to my email, please!"
);
//val messageBodyPart = MimeBodyPart();
//val multipart = MimeMultipart();
//val file = "path of file to be attached";
// val fileName = "attachmentName"
// val source = FileDataSource(file);
//messageBodyPart.setDataHandler(DataHandler(source));
//messageBodyPart.setFileName(fileName);
//multipart.addBodyPart(messageBodyPart);
//message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (e: MessagingException) {
throw RuntimeException(e);
}
}
}
编辑:JavaMail 1.5.5声称支持Android,所以你不需要其他任何东西。
我已经将最新的JavaMail(1.5.4)移植到Android。它可以在Maven Central中获得,只需添加以下内容到build.gradle~~
compile 'eu.ocathain.com.sun.mail:javax.mail:1.5.4'
然后你就可以按照官方教程学习了。
源代码可以在这里找到:https://bitbucket.org/artbristol/javamail-forked-android
这里是一个Alt版本,也适用于我,并有附件(已经张贴在上面,但完整的版本不像源链接,人们张贴他们不能让它工作,因为它缺少数据)
import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
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;
public class Mail extends javax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // default smtp port
_sport = "465"; // default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public Mail(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
// the getters and setters
public String getBody() {
return _body;
}
public void setBody(String _body) {
this._body = _body;
}
public void setTo(String[] toArr) {
// TODO Auto-generated method stub
this._to=toArr;
}
public void setFrom(String string) {
// TODO Auto-generated method stub
this._from=string;
}
public void setSubject(String string) {
// TODO Auto-generated method stub
this._subject=string;
}
// more of the getters and setters …..
}
在活动中调用它…
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button addImage = (Button) findViewById(R.id.send_email);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Mail m = new Mail("gmailusername@gmail.com", "password");
String[] toArr = {"bla@bla.com", "lala@lala.com"};
m.setTo(toArr);
m.setFrom("wooo@wooo.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");
try {
m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
});
}