当前位置: 首页 > 知识库问答 >
问题:

Android studio邮件问题

纪晨
2023-03-14

我已经在我的应用程序中设置了类,根据用户的姓名电子邮件和消息发送邮件。它在应用程序中似乎运行良好,但没有实际的电子邮件被发送。有人能帮忙吗?事先谢谢你。

以下是我设置的Gmail类:

公共级GMail{

final String emailPort = "587"; // this is gMail's smtp port number
final String smtpAuth = "true";
final String starttls = "true";
final String emailHost = "smtp.gmail.com";
String fromEmail;
String fromPassword;
List<String> toEmailList;
String emailSubject;
String emailBody;
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

// this will be the constructor of the email
public GMail(String fromEmail, String fromPassword, List<String> toEmailList, String emailSubject, String emailBody)
{
    this.fromEmail = fromEmail;
    this.fromPassword = fromPassword;
    this.toEmailList = toEmailList;
    this.emailBody = emailBody;

    //setting the server settings for Gmail
    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", smtpAuth);
    emailProperties.put("mail.smtp.starttls.enable", starttls);
    Log.i("Gmail", "Mail server properties are now set.");

}

public MimeMessage createEmailMessage() throws AddressException,
        MessagingException, UnsupportedEncodingException{
        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);
        emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));//address setup
        for(String toEmail : toEmailList){
            Log.i("GMail", "toEmail" + toEmail);
            emailMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toEmail));
        }
        emailMessage.setSubject(emailSubject); //email Subject
        emailMessage.setContent(emailBody, "text/html");
        return emailMessage;
}

public void sendEmail() throws AddressException, MessagingException
{
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(emailHost, fromEmail, fromPassword);
    Log.i("Gmail", "allrecipients: " + emailMessage.getAllRecipients());
    transport.close();
    Log.i("Gmail", "Your Email has successfully been sent.");
}

}

这是我的SendMailTask课程

公共类SendMailTask扩展了AsyncTask{

private ProgressDialog statusDialog;
private Activity sendMailActivity;

public SendMailTask(Activity activity) {
    sendMailActivity = activity;
}

// make the method for showing dialog progress
protected void onPreExecute() {
    statusDialog = new ProgressDialog(sendMailActivity);
    statusDialog.setMessage("Setting up..");
    statusDialog.setIndeterminate(false);
    statusDialog.setCancelable(false);
    statusDialog.show();
}
//method for creation and sending of email
@Override
protected Object doInBackground(Object... args) {
    try{
        Log.i("SendMailTask", "Setting up your email ..");
        publishProgress("Processing all your information ..");
        GMail androidEmail = new GMail(args[0].toString(),
                args[1].toString(), (List) args[2], args[3].toString(),
                args[4].toString());
        publishProgress("Preparing your information ..");
        androidEmail.createEmailMessage();
        publishProgress("Sending your information ..");
        androidEmail.sendEmail();
        publishProgress("Information sent.");
        Log.i("SendMailTask", "Information sent.");

    }catch(Exception e){
        publishProgress(e.getMessage());
        Log.e("SendMailTask", e.getMessage(), e);
    }
    return null;
}
// creating method for dialog messages.
@Override
public void onProgressUpdate(Object... values){
    statusDialog.setMessage(values[0].toString());
}
//method to get rid of the dialog message.
@Override
public void onPostExecute(Object result){
    statusDialog.dismiss();
}

}

下面是我试图从中获得邮件的班级:

公共类HelpPage扩展AppCompatActive{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help_page2);

    final EditText mail1 = findViewById(R.id.email1);
    final EditText mail2 = findViewById(R.id.email2);
    final EditText nameIn = findViewById(R.id.hName1);
    final Button btnSub = findViewById(R.id.helpBtn);
    final EditText msgIn = findViewById(R.id.hMsg);
    final String password = "xxxxxxxx";
    final String sendemail = "come2gopj@gmail.com";
    final String recemail = "c2grecieve@gmail.com";
    final boolean connt;

    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED){
        // Connection affirmative:)
        connt = true;
    }
    else {
        connt = false;
        Toast.makeText(getApplicationContext(), "You're not connected to the internet",
                Toast.LENGTH_LONG).show();
    }
    btnSub.setOnClickListener(
            arg0 -> {
                //method to make sure all fields are checked
                if(nameIn.getText().toString().equals("")||mail1.getText().toString().equals("")||mail2.getText().toString().equals("")||msgIn.getText().toString().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Please check you've filled all fields.",
                            Toast.LENGTH_LONG).show();
                }
                else if(connt!=true){
                    Toast.makeText(getApplicationContext(), "You're internet connection needs to be checked",
                            Toast.LENGTH_LONG).show();
                }
                else{
                    if(mail1.getText().toString().equals(mail2.getText().toString())){
                        Log.i("SendMailActivity", "Send Button Clicked.");
                        //declaring the recieving, password and sending of the email
                        List<String> toEmailList = Arrays.asList(recemail.split("\\s*,\\s*")); //Recipient List
                        Log.i("SendMailActivity", "To List: " + toEmailList);
                        String emailSubject = ((EditText) findViewById(R.id.hName1)).getText().toString();
                        String emailEmail = ((EditText) findViewById(R.id.email1)).getText().toString();
                        String emailBody = "User's Email : " + ((EditText) findViewById(R.id.email1)).getText().toString() + "\n" +
                                "User's message: "+((EditText) findViewById(R.id.hMsg)).getText().toString();
                        new SendMailTask(HelpPage.this).execute(sendemail, password,toEmailList,emailSubject,emailBody,emailEmail);//send the email with all the relevant data included
                        startActivity(new Intent(getApplicationContext(),AfterMail.class)); //this will start the next activity that i have included after the mail is sent
                    }
                    else{//method for checking the email inputs
                        Toast.makeText(getApplicationContext(), "Your emails don't match please try again.",
                                Toast.LENGTH_LONG).show();
                    }


                }

            });

}

以下是我按下“发送”按钮后在“运行”对话框中收到的消息:

我/发送邮件活动:发送按钮点击。我/发送邮件活动:要列表:[c2grecieve@gmail.com]

我/发送邮件任务:设置您的电子邮件...

I/Gmail:邮件服务器属性现在已设置。

D/EGL_仿真:eglMakeCurrent:0xe1205120:版本3 1(tinfo 0xe1203320)

I/。myapplication:后台并发复制GC释放24080(6MB)AllocSpace对象,5(92KB)LOS对象,49%空闲,2MB/4MB,暂停1.019ms总计105.897ms

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

W/ActivityThread: handleWindowVitivity:没有令牌android.os.BinderProxy@af22bc6活动

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

I/chatty:uid=10085(com.example.myapplication2)渲染读取相同的5行

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

I/GMail:toEmailc2grecieve@gmail.comD /EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

我/编舞:跳过了34帧!应用程序可能在其主线程上做了太多工作。

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/NetworkSecurityConfig:未指定网络安全配置,使用平台默认设置

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

D/EGL_emulation: eglMake电流: 0xe1205120: ver 3 1(tinfo 0xe1203320)

I/Gmail:所有收件人:[Ljavax.mail.internet.InternetAddress;@80121b1

I/Gmail:您的电子邮件已成功发送。I/SendMailTask:已发送信息。

共有1个答案

贲宏硕
2023-03-14

我终于知道我错过了什么。在GMail.java文件中,我缺少一行代码来获得实际发送的电子邮件。transport.send消息(emailMessage,emailMessage.getAllRecipients());

这就是我错过的那条线`

 类似资料:
  • 问题内容: 尝试使用Java邮件时,我收到以下异常; 广泛搜索堆栈溢出后(也许不够广泛,让我们看看!),我发现我们需要在maven pom内放入两个jar来完成这项工作。我的两个依赖关系如下: 某些情况已更改,但我不完全确定它是什么-之前已奏效。我在该元素失败的地方编写的代码如下: 我是否缺少某些配置,还是应该使用我当前的设置? 谢谢 问题答案: 尝试将其添加到Maven POM中:

  • 大家好抱歉我是AWS的新手, 我有麻烦从AWS发送电子邮件地址。 以下是我的配置详细信息: 1)我已经成功验证了我的电子邮件地址和域名。 2) 同样,我也通过以下策略添加IAM用户 {"语句": [{ "效果":"允许","操作":"ses: SendRawEmail","资源":"*" }]} 3)您可以看到添加邮件地址已验证 但我想我不知道为什么我在尝试发送邮件时会出现以下错误。 我收到以下错

  • 问题内容: 我正在建立一个网站,该网站在用户注册时向其发送电子邮件。 我的代码(要点): 问题在于,当邮件被传递时,发件人头仍然保留,而回复对象更改为指定值。 是托管网站的服务器的主机名。 那我在做什么错?如何获得“发件人”地址和回复地址相同的地址? 是我做错了什么,还是网络主机在犯规? 问题答案: 编辑:我刚刚注意到您正在尝试使用gmail地址作为from值。 这是行不通的 ,ISP正确地覆盖了

  • 我正在编写一个具有交叉源配置的API 我的websecurity配置有 控制器类 身份验证过滤器检查 匹配。匹配总是变假 我是不是漏掉了什么? 我添加了什么额外的参数吗? 期待着迅速的回应。

  • 我们有一个哈德逊的老版本(版本1.379,是的,就是那个老版本…)这还没有升级(我不确定什么时候以及是否会由负责人升级)。 似乎有一些地方,显然无法从网络界面访问,添加了额外的电子邮件ext插件收件人。。。 问题是,这些收件人已不复存在,相当多的人(大多与使用哈德逊系统的人无关)会收到这些邮件的回帖。。。 对于电子邮件文本,我在插件页面中得到这个: 版本: 2.12安装: 2.8 我已经看了配置,

  • 我有一个问题,过多的填充出现在表周围或移动图像与超文本标记语言的电子邮件签名,我无法找到解决方案。 我必须尝试我研究过的每一个解决方案,包括: 将显示:块添加到图像中 这个问题似乎出现在我测试过的三星和苹果设备上。 如有任何新建议,将不胜感激。 特别具有额外间距的图像如下所示: 我有两个并列的表,这可能是问题的原因,因此整个代码是怎样的: