gpt4 book ai didi

java - Android studio 邮件问题

转载 作者:行者123 更新时间:2023-12-02 09:17:37 24 4
gpt4 key购买 nike

我已经在我的应用程序中设置了类,以根据用户的姓名、电子邮件和消息发送邮件。在应用程序中似乎工作正常,但没有发送实际的电子邮件。有人可以帮忙吗?预先感谢您。

这是我设置的 Gmail 类:

公共(public)类 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​​ 类

公共(public)类 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();
}

}

这是我试图获取发送邮件的类:

公共(public)类HelpPage扩展了AppCompatActivity{

@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();
}


}

});

}

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

I/SendMailActivity:单击发送按钮。I/SendMailActivity:到列表:[c2grecieve@gmail.com]

I/SendMailTask​​:设置您的电子邮件..

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

D/EGL_emulation: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:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

W/ActivityThread:handleWindowVisibility: token android.os.BinderProxy@af22bc6 没有 Activity

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

I/chatty: uid=10085(com.example.myapplication2) RenderThread 相同 5 行

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

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

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

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

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

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

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

D/EGL_emulation:eglMakeCurrent:0xe1205120:版本 3 1(tinfo 0xe1203320)

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

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

最佳答案

我终于知道我错过了什么了。在 GMail.java 文件中,我缺少 1 行代码来实际发送电子邮件。 `transport.sendMessage(emailMessage, emailMessage.getAllRecipients());

那是我错过的那一行。 `

关于java - Android studio 邮件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com