我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
当前回答
这里有很多解决方案。然而,我认为我们必须改变GMail的配置,以允许从不太安全的设备访问。转到下面的链接并启用它。这对我很有用
https://myaccount.google.com/lesssecureapps?pli=1
其他回答
要加附件,别忘了加。
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);
我为其他需要帮助的人找到了一个更短的选择。代码是:
package com.example.mail;
import java.util.Properties;
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;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
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");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
来源:通过JavaMail API发送电子邮件
希望这能有所帮助!好运!
编辑: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
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);
}
}
}
那些正在获得ClassDefNotFoundError的人尝试移动这三个jar文件到你的项目的lib文件夹,它为我工作!!