Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SMTP通过javax.mail从Android发送电子邮件_Android_Jakarta Mail - Fatal编程技术网

如何使用SMTP通过javax.mail从Android发送电子邮件

如何使用SMTP通过javax.mail从Android发送电子邮件,android,jakarta-mail,Android,Jakarta Mail,我正在使应用程序具有通过SMTP协议从应用程序发送电子邮件的功能。 我想让它在后台从应用程序发送电子邮件,而不是像使用其他邮件应用程序发送的意图。 我在谷歌上搜索时,有一些关于javax.mail的建议 我有SMTP服务器的信息:主机,端口,用户名,密码 我如何使用javax.mail通过SMTP向收件人发送包含html的电子邮件,并且电子邮件也附带了文件 如果您想使用javax.mail发送电子邮件,请感谢。您可以使用以下帮助器类发送电子邮件。不管它是否由后台服务发送,也不管它是否由异步任务发

我正在使应用程序具有通过SMTP协议从应用程序发送电子邮件的功能。 我想让它在后台从应用程序发送电子邮件,而不是像使用其他邮件应用程序发送的意图。 我在谷歌上搜索时,有一些关于
javax.mail
的建议

我有SMTP服务器的信息:主机,端口,用户名,密码

我如何使用javax.mail通过SMTP向收件人发送包含html的电子邮件,并且电子邮件也附带了文件


如果您想使用
javax.mail
发送电子邮件,请感谢。您可以使用以下帮助器类发送电子邮件。不管它是否由后台服务发送,也不管它是否由异步任务发送。看一下下面课程中的配置部分

package com.company.mail.util;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
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;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class Mail {
    public static final String EMAIL_LIST_SEPARATOR = ",";

    public boolean sendMail(String[] toEmails, String subject, String msg, boolean isHTML, String charSet, String filename, byte[] attachment) {
    if (toEmails == null || (toEmails != null && toEmails.length == 0))  return false;

    String name = "My Company Name";
    String username = "no-reply@company.com"; 
    String password = "<My Password>"; 
    String host = "smtp.company.com";
    Integer port = 465;
    String from = "no-reply@company.com";

    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.socketFactory.port", port.toString());
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", port.toString());

    final String fUsername = username;
    final String fPassword = password;
    Session mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(fUsername, fPassword);
        }
    });
    //mailSession.setDebug(true);

    String recipients = ArrayUtils.toString(toEmails, "");
    Message message = new MimeMessage(mailSession);
    try {
        message.setFrom(new InternetAddress(from, name));
        message.setReplyTo(new InternetAddress[] { new InternetAddress(from, name) });
        message.setSubject(subject);
        message.setContent(msg, isHTML ? "text/html" : "text/plain");

        if (toEmails != null) {
            for (String recipient : toEmails) {
                if (recipient != null && !"".equals(recipient)) {
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
                }
            }

            if (attachment != null) {
                MimeBodyPart messagePart = new MimeBodyPart();
                if (isHTML)
                    messagePart.setContent(msg, "text/html");
                else
                    messagePart.setText(msg);

                MimeBodyPart attachmentPart = new MimeBodyPart();
                attachmentPart.setDataHandler(new DataHandler(attachment, "application/octet-stream"));
                attachmentPart.setFileName(filename);

                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messagePart);
                multipart.addBodyPart(attachmentPart);

                message.setContent(multipart);
            }

            Transport.send(message);
            Log.i(" [Mail] Recipient " + name + " <" + username + "> sent e-mail with subject " + subject + " to recipient(s): " + recipients);

            return true;
        }
    } catch (Exception e) {
        Log.e(" [Mail] Error sending " + toEmails.length  + " e-mail(s) with subject '" + subject + "' to recipient(s) (" + recipients + "): " + e.getMessage());
    }

    return false;
    }
}
此类还支持向邮件发送附件,因此您可以将文件名和字节数组作为最后两个参数传递


希望这个Java类对您有所帮助。

您想在应用程序打开时发送吗?或者你想在不打开应用程序的情况下发送(通过后台服务发送)?我想在应用程序打开的情况下发送电子邮件,并在后台服务中发送每日电子邮件。
Mail mail = new Mail();
mail.sendMail(new String[] { "someone@somewhere.com", "john.doe@mycompany.com" }, "Subject of my e-mail", "This is a message in <b>HTML</b> with <i>italics</i>.", true, "UTF-8", null, null);