android - 我可以使用 GoogleSignInApi 和 SMTP 服务器(如果可能)或以其他方式发送没有密码的电子邮件吗?

标签 android

我正在创建一个简单的 android 应用程序,用于发送适用于语音输入的百叶窗电子邮件。应用程序不与发送邮件的普通 gmail/电子邮件应用程序交互。我使用 GoogleSignInApi 来避免无效的电子邮件 ID 和发件人电子邮件和密码的静态/动态输入(安全目的)。使用 GoogleSignInApi 我可以获取电子邮件地址,但主要问题是我无法从 api 获取密码,因此我无法发送电子邮件。我可以在 Android 中不使用密码发送电子邮件吗?
提前致谢..

主 Activity .java

private void handleResult(GoogleSignInResult result)
{
    if(result.isSuccess())
    {
        GoogleSignInAccount account = result.getSignInAccount();
        String name = account.getDisplayName();
        String email =  account.getEmail();
        String img_url=account.getPhotoUrl().toString();
        Name.setText(name);
        Email.setText(email);
        Glide.with(this).load(img_url).into(Prof_pic);
        updateUI(true);
    }
}

配置.java

//For storing email address and password of sender

public class Config 
{

    public static String EMAIL ="youremail@gmail.com";  //gmail address

    public static String PASSWORD ="passwd"; //password
}

发送邮件.java

public class SendMail extends AsyncTask<Void,Void,Void> {

    //Declaring Variables
    private Context context;
    private Session session;

    //Information to send email
    private String email;
    private String subject;
    private String message;
    private TextToSpeech t1;

    //Progressdialog to show while sending email
    private ProgressDialog progressDialog;


    //Class Constructor
    public SendMail(Context context, String email, String subject, String message){
        //Initializing variables
        this.context = context;
        this.email = email;
        this.subject = subject;
        this.message = message;
        t1=new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Showing progress dialog while sending email
        progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //Dismissing the progress dialog
        progressDialog.dismiss();
        //Showing a success message
        Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
        t1.speak("Message Sent", TextToSpeech.QUEUE_FLUSH, null);
    }

    @Override
    protected Void doInBackground(Void... params) {
        //Creating properties
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");

        props.setProperty("mail.host", "smtp.gmail.com");

        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");

        //Configuring properties for gmail
        //If you are not using gmail you may need to change the values
        //props.put("mail.smtp.host", "smtp.gmail.com");
        //props.put("mail.smtp.socketFactory.port", "465");
        //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        //props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.port", "465");

        //Creating a new session
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    //Authenticating the password
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(Config.EMAIL,Config.PASSWORD);
                    }
                });

        try {
            //Creating MimeMessage object
            MimeMessage mm = new MimeMessage(session);

            //Setting sender address
            mm.setFrom(new InternetAddress(Config.EMAIL));
            //Adding receiver
            mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            //Adding subject
            mm.setSubject(subject);

            //Adding message
            mm.setText(message);

            //Sending email
            Transport.send(mm);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

最佳答案

Can i send email without using password in Android?

没有。否则,垃圾邮件发送者只会使用 Android 设备发送他们的电子邮件。

您正在创建一个电子邮件客户端。电子邮件客户端需要帐户凭据和服务器信息(SMTP、IMAP 等)才能发送和接收电子邮件。

您可能会考虑将您的功能添加到现有的开源电子邮件客户端(例如,K9 Mail),或者至少使用一个作为您的应用程序需要成为电子邮件客户端的各种事情的指南。

关于android - 我可以使用 GoogleSignInApi 和 SMTP 服务器(如果可能)或以其他方式发送没有密码的电子邮件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150947/

相关文章:

android - 评估项目应用程序时出现问题-Firebase Analytics

Android - 使用 Joda Time 时无法使用 Proguard 导出 apk

android - 在 flutter 应用程序中添加日期选择器的正确方法是什么?

android - 短信收件箱 - ListView

android - 如何在我的android Activity 中找出哪个 Activity 已经开始 Intent

java - 找不到使用操作和数据处理 Intent 的 Activity

android - 如何在 Android 和计算机之间发送/接收文本?

android - 在 Android Lollipop 中膨胀类 android.support.v7.widget.Toolbar 时出错

android - 如何在 android API <21 中检查省电模式

java - 使用 FragmentManager 和 FragmentTransaction 将 Map Fragment 动态添加到 Activity 中