android - 我可以使用 GoogleSignInApi 和 SMTP 服务器(如果可能)或其他方式发送没有密码的电子邮件吗?
问题描述
我正在创建一个简单的 android 应用程序,用于发送适用于语音输入的百叶窗电子邮件。应用程序不与普通 gmail/电子邮件应用程序交互以发送邮件。我使用 GoogleSignInApi 来避免无效的电子邮件 ID 和发件人电子邮件和密码的静态/动态输入(安全目的)。使用 GoogleSignInApi 我可以获取电子邮件地址,但主要问题是我没有从 api 获取密码,因此我无法发送电子邮件。我可以在 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);
}
}
配置文件
//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;
}
}
解决方案
我可以在 Android 中不使用密码发送电子邮件吗?
不会。否则,垃圾邮件发送者只会使用 Android 设备发送电子邮件。
您正在创建一个电子邮件客户端。电子邮件客户端需要帐户凭据和服务器信息(SMTP、IMAP 等)才能发送和接收电子邮件。
您可能会考虑将您的功能添加到现有的开源电子邮件客户端(例如,K9 Mail),或者至少使用一个作为您的应用程序作为电子邮件客户端所需的各种功能的指南。
推荐阅读
- sql-server - 变量作为运算符 mssql
- python - 通过将旧值连接到另一个值来更新所有值数据框 Pandas
- python - Python df groupby 带有用于字符串和总和的 agg
- xml - 在定期更新的 XSD 中替换 xsd:choice
- python - pd.to_datetime 命令月份错误
- python - 哪个文件包含我的深度学习保存模型?
- c# - 升级到生产计划时 Azure 应用服务不工作
- tensorflow - tf.keras.layers.Concatenate() 使用列表但在张量元组上失败
- c# - 从 DataTable 中检索数据作为单行参数
- google-cloud-platform - 使用 Tableau Server 设置 Google 服务帐户