我正在创建一个简单的安卓应用程序为盲人发送电子邮件,这对语音输入的工作。应用程序不与正常的gmail/电子邮件应用程序交互来发送邮件。我使用GoogleSignInApi来避免无效的电子邮件id和静态/动态输入发件人的电子邮件和密码(安全目的)。使用GoogleSignInApi我可以获取电子邮件地址,但主要问题是我无法从应用程序接口获取密码,因此我无法发送电子邮件。Android下可以不使用密码发送邮件吗?
提前谢谢..
MainActivity.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);
}
}Config.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
}SendMail.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;
}
}发布于 2019-01-12 01:24:02
在安卓系统下可以不使用密码发送邮件吗?
不是的。否则,垃圾邮件发送者只会使用Android设备发送电子邮件。
您正在创建电子邮件客户端。电子邮件客户端需要帐户凭据和服务器信息(SMTP、IMAP等)以便发送和接收电子邮件。
您可以考虑将您的功能添加到现有的开源电子邮件客户端(例如,K9邮件),或者至少使用其中一个作为您的应用程序作为电子邮件客户端所需的各种东西的指南。
https://stackoverflow.com/questions/54150947
复制相似问题