大家好我正在使用JavaMailApi我正在尝试从我的应用程序发送电子邮件。这个问题已经被问了很多次了,我已经尝试了很多这样的例子,几乎所有的都是相同但我无法解决javax.mail.MessagingException的异常:无法连接到SMTP主机:smtp-mail.outlook.com,port: 587 这是我的代码:
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;
//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;
}
@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();
}
@Override
protected Void doInBackground(Void... params) {
String prot = "smtp";
final String user = Config.EMAIL;
String to = Config.EMAIL;
final String pass = Config.PASSWORD;
String mailhost = "smtp-mail.outlook.com";
Properties props = System.getProperties();
props.put("mail." + prot + ".host", mailhost);
props.put("mail." + prot + ".auth", "true");
props.put("mail." + prot + ".starttls.enable", "true");
props.put("mail." + prot + ".port", "587");
props.put("mail." + prot + ".ssl.enable", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
session.setDebug(true);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject("TESTE ENVIO EMAIL");
msg.setText("Teste de envio de email conteudo");
msg.setHeader("X-Mailer", user);
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport(prot);
t.connect(mailhost, user, pass);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
Log.d("------------", "done");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
请告诉我这段代码有什么问题。 谢谢
答案 0 :(得分:0)
您好请将此代码放在doInBackground方法中,尝试可能会对您有所帮助。它为我工作
try {
Properties props = new Properties();
props.put("mail.smtp.auth", Constant.MAIL_SMTP_AUTH);
props.put("mail.smtp.starttls.enable", Constant.MAIL_SMTP_STARTTLS_ENABLE);
props.put("mail.smtp.host", Constant.MAIL_SMTP_HOST);
props.put("mail.smtp.port", Constant.MAIL_SMTP_PORT);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Constant.SMTP_USER, Constant.SMTP_PASSWORD);
}
});
message = new MimeMessage(session);
message.setFrom(new InternetAddress(Constant.SMTP_FROM));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(Constant.SMTP_TO));
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(Constant.SMTP_CC));
message.setSubject(mSalesOrderNumber + "_Invoice");
String messageStr = getString(R.string.msg_please_find_attached_invoice);
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(messageStr);
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filePath) {
@Override
public String getContentType() {
return "application/pdf";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filePath.getName());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
} catch (Exception e) {
e.printStackTrace();
}