gpt4 book ai didi

android - 在 Android 应用程序中发送自动电子邮件的问题

转载 作者:行者123 更新时间:2023-11-29 21:14:17 25 4
gpt4 key购买 nike

我想从我的 Android 应用程序发送电子邮件,所以我的代码是

public class MainActivity extends Activity  implements OnClickListener{

Session session=null;
ProgressDialog pdialog=null;
Context context=null;
EditText reciept=null;
EditText sub=null;
EditText msg=null;
String recpient=null;
String subject=null;
String textmessage=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
Button login = (Button) findViewById(R.id.mBtnSubmit);
reciept=(EditText)findViewById(R.id.editText_to);
sub = (EditText) findViewById(R.id.editText_sub);
msg = (EditText) findViewById(R.id.editText_text);


login.setOnClickListener(this);


}

@Override
public void onClick(View v) {

recpient= reciept.getText().toString();
subject= sub.getText().toString();
textmessage= msg.getText().toString();

Properties props = new Properties();
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");


session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@a.a", "password");
}
});
pdialog = ProgressDialog.show(context, "", "Sending Mail...",true);
RetreiveFeedTask task= new RetreiveFeedTask();
task.execute();
}


class RetreiveFeedTask extends AsyncTask<String, Void, String> {


protected String doInBackground(String... urls) {
try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@a.a"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recpient));
message.setSubject(subject);
message.setContent(textmessage, "text/html; charset=utf-8");

Transport.send(message);


}
catch (MessagingException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();

}
return null;
}

protected void onPostExecute(String feed) {
pdialog.dismiss();
reciept.setText("");
msg.setText("");
sub.setText("");
Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();

}
}


}

而且我还附加了 jar

activation.jar
additionnal.jar
mail.jar
android-support-v4.jar

当我运行上面的代码时,我给了我这样的错误

02-18 16:16:05.242: W/System.err(3242): javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
02-18 16:16:05.242: W/System.err(3242): nested exception is:
02-18 16:16:05.242: W/System.err(3242): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
02-18 16:16:05.242: W/System.err(3242): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
02-18 16:16:05.242: W/System.err(3242): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
02-18 16:16:05.242: W/System.err(3242): at javax.mail.Service.connect(Service.java:310)
02-18 16:16:05.242: W/System.err(3242): at javax.mail.Service.connect(Service.java:169)
02-18 16:16:05.242: W/System.err(3242): at javax.mail.Service.connect(Service.java:118)
02-18 16:16:05.242: W/System.err(3242): at javax.mail.Transport.send0(Transport.java:188)

我该如何解决?

最佳答案

package com.example.maler;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Neamail extends Activity{

Button mButton;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton=(Button)findViewById(R.id.button1);
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
sendEmail();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void sendEmail() throws AddressException, MessagingException {
String host = "smtp.gmail.com";
String address = "senderaddress@gmail.com";

String from = "senderaddress@gmail.com";
String pass = "sender pass";
String to="receiver@address.com";

Multipart multiPart;
String finalString="";

Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", address);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Log.i("Check", "done pops");
Session session = Session.getDefaultInstance(props, null);
DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setDataHandler(handler);
Log.i("Check", "done sessions");

multiPart=new MimeMultipart();

InternetAddress toAddress;
toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
Log.i("Check", "added recipient");
message.setSubject("Send Auto-Mail");
message.setContent(multiPart);
message.setText("Demo For Sending Mail in Android Automatically");

Log.i("check", "transport");
Transport transport = session.getTransport("smtp");
Log.i("check", "connecting");
transport.connect(host,address , pass);
Log.i("check", "wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Log.i("check", "sent");

}

关于android - 在 Android 应用程序中发送自动电子邮件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850282/

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