我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
我试图在Android中创建一个邮件发送应用程序。
如果我使用:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
这将启动内置的Android应用程序;我试图发送邮件按钮点击直接不使用这个应用程序。
当前回答
你考虑过使用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/
其他回答
要加附件,别忘了加。
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);
在Android中使用JavaMail API使用Gmail身份验证发送电子邮件。
创建示例项目的步骤:
MailSenderActivity.java:
public class MailSenderActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
GMailSender sender = new GMailSender("username@gmail.com", "password");
sender.sendMail("This is Subject",
"This is Body",
"user@gmail.com",
"user@yahoo.com");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
});
}
}
GMailSender.java:
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
JSSEProvider.java:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Alexander Y. Kleymenov
* @version $Revision$
*/
import java.security.AccessController;
import java.security.Provider;
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
将下面链接中的3个罐子添加到你的Android项目中
mail.jar activation.jar additional.jar
点击这里-如何添加外部罐子
别忘了在你的清单上加上这一行:
<uses-permission android:name="android.permission.INTERNET" />
只需点击下面的链接,更改帐户访问不太安全的应用程序 https://www.google.com/settings/security/lesssecureapps
运行项目并检查收件人邮件帐户。
另外,别忘了你不能在android的任何活动中进行网络操作。 因此,建议使用AsyncTask或IntentService来避免主线程网络异常。
Jar文件:https://code.google.com/archive/p/javamail-android/
用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" />
谢谢你提供的宝贵信息。代码运行正常。我也可以通过添加以下代码添加附件。
private Multipart _multipart;
_multipart = new MimeMultipart();
public void addAttachment(String filename,String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);
_multipart.addBodyPart(messageBodyPart2);
}
message.setContent(_multipart);
其他答案中提供的所有代码都是正确的,运行正常,但有点混乱,所以我决定发布一个库(尽管仍在开发中),以更简单的方式使用它: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'
如果你有任何问题,请告诉我!:)