Android应用程序开发-直接发送电子邮件而无需意图

6

有没有人能建议我如何在不使用意图或打开其他Android应用程序的情况下发送电子邮件。 我希望一旦我点击发送按钮,邮件就直接发送。 我必须使用特定的office365电子邮件来发送消息。 我应该使用某些API或SMTP等吗?

是否有简单的方法可以在我的Android应用程序上应用此功能?


1
http://www.edumobile.org/android/send-email-on-button-click-without-email-chooser/ - Pavya
先生,它能在Office365账户上运行吗?或者除了Gmail之外的任何账户?谢谢。 - APX
https://github.com/OfficeDev/Office-365-SDK-for-Android - Quick learner
我应该为Microsoft Azure账户/注册工作以获取权限吗?太麻烦了 :( 谢谢 - APX
2个回答

1

它在我这里不起作用。我的应用程序停止了。我将所有的“youremail@gmail.com”电子邮件和密码更改为我的自己的帐户。请帮忙。 - APX
1
@APX,你解决了吗?当我使用自己的Gmail地址和密码时,我的应用程序也遇到了同样的问题。我还启用了Gmail中的“允许不安全设备访问”设置。 - user3451660

1

你好 @APX, 以下是两个类,可以帮助你发送电子邮件给单个或多个用户,无需任何库。我认为这会对你有所帮助。

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

public class MailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new JSSEProvider());
    }

    public MailSender(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 from, String recipients) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/html"));
            /*message.setSender(new InternetAddress(sender, "Dhaval Solanki"));*/
            message.setFrom(new InternetAddress(sender, from));
            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");
        }
    }
}





*  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;
            }
        });
    }
}

你可以像下面这样使用:
MailSender sender = new MailSender("" + authEmail, "" + authPwd);
                    sender.sendMail("" + subject,
                            "" + prepareMailText(name),
                            "" + authEmail, "WeMeal Team",
                            "" + emailList); //email list send comma separated string like ("a@gmail.com,b@dahs.com")

我宁愿使用Office365电子邮件发送消息。这是可能的吗?我必须更改此代码中的哪些参数,例如某些端口或主机等。谢谢! - APX
嗨,如果您尝试使用Office 365并且无法正常工作,请在类构造函数中添加以下行: props.put("mail.smtp.host", "m.outlook.com"); 以下链接将对您有所帮助:https://dev59.com/qGYq5IYBdhLWcg3wfwxx - Dhaval Solanki
好的,谢谢。还有一个问题,你是怎么声明你的 "emailId[0]" 的?谢谢。 - APX
你好,这里不需要声明数组对象,只需像这样传递字符串作为参数:"sample@gmail.com,asdaadad@gmail.com,asdaadad@gmail.com"。如需更多信息,请查看我的更新答案。 - Dhaval Solanki
sender.sendMail("" + subject, "" + prepareMailText(name), "" + authEmail, "WeMeal 团队", "sample@gmail.com,asdaadad@gmail.com,asdaadad@gmail.com"); 仍然出现错误。未处理的异常:java.lang.Exception - APX
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接