在VPN上无法发送电子邮件(smtp)

时间:2019-03-03 09:45:22

标签: android smtp

我通过SMTP将电子邮件从应用程序发送到我的Gmail帐户。它可以成功工作,但是在VPN开启时,电子邮件不会发送。如何在Android Studio中解决此问题? 还有另一种无需使用意图就可以在android studio中将消息发送到我的电子邮件的方法吗?

Sendmail.java类:

public class SendMail extends AsyncTask<Void, Void, Void> {
private Context context;
private Session session;
private String email;
private String subject;
private String message;
private String name;
private String youremail;
private ProgressDialog progressDialog;
public SendMail(Context context, String name, String youremail, String email, String subject, String message) {
    this.context = context;
    this.email = email;
    this.subject = subject;
    this.message = message;
    this.name = name;
    this.youremail = youremail;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false); 
}
@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {
    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(Config.EMAIL, Config.PASSWORD);
                }
            });
    try {
        MimeMessage mm = new MimeMessage(session);
        mm.setFrom(new InternetAddress(Config.EMAIL));
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        mm.setSubject(subject);
        mm.setText(name + "\n" + youremail + "\n" + message);
        Transport.send(mm);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return null;
}}

0 个答案:

没有答案