JavaMail和电子邮件帐户的连接
我尝试使用javamail连接电子邮件-在非android应用程序中,此代码运行良好:JavaMail和电子邮件帐户的连接,java,android,email,jakarta-mail,Java,Android,Email,Jakarta Mail,我尝试使用javamail连接电子邮件-在非android应用程序中,此代码运行良好: public void perform(View v){ Toast.makeText(getApplicationContext(), "Validating...", Toast.LENGTH_SHORT).show(); new LoginIn().execute(); } private class LoginIn extends AsyncT
public void perform(View v){
Toast.makeText(getApplicationContext(), "Validating...",
Toast.LENGTH_SHORT).show();
new LoginIn().execute();
}
private class LoginIn extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.stmp.user", "sample@gmail.com");
//If you want you use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password of the sender");
// If you want to use SSL
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");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
Transport trans;
try {
trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 25, "sample@gmail.com", "asdasd");
} catch (NoSuchProviderException e) {
e.printStackTrace();
}catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
我已经读到我应该在asynctask中这样做,所以我做了,但没有帮助。有人能帮我吗?应用程序告诉我“应用程序可能在其主线程上做了太多的工作”,但我尝试在其他线程中这样做?您的类路径设置正确吗
Caused by: java.lang.NoClassDefFoundError: javax.mail.Session
我在java项目中使用了mail.smtp。 下面是我帮助您的代码
package com.agileinfotech.bsviewer.service;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailUtil {
public final String username = "abcxyz@gmail.com";
public final String password = "******";
public void mailSuccAtt(String sch_Id, String jb_status, String filename,String documentName, String mail) {
System.out.println("in the mail util tname of list is " + mail);
try {
if (jb_status.equals("Completed")) {
Properties pros = new Properties();
pros.put("mail.smtp.auth", "true");
pros.put("mail.smtp.starttls.enable", "true");
pros.put("mail.smtp.host", "smtp.gmail.com");
pros.put("mail.smtp.port", "587");
Session session = Session.getInstance(pros,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username, password);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
mail));
msg.setSubject("Attached Demo");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hello " + "\n\n Status of " + sch_Id
+ " is " + jb_status);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(documentName);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
//System.out.println("Mail Sent");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.agileinfotech.bsviewer.service;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailUtil {
public final String username = "abcxyz@gmail.com";
public final String password = "******";
public void mailSuccAtt(String sch_Id, String jb_status, String filename,String documentName, String mail) {
System.out.println("in the mail util tname of list is " + mail);
try {
if (jb_status.equals("Completed")) {
Properties pros = new Properties();
pros.put("mail.smtp.auth", "true");
pros.put("mail.smtp.starttls.enable", "true");
pros.put("mail.smtp.host", "smtp.gmail.com");
pros.put("mail.smtp.port", "587");
Session session = Session.getInstance(pros,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username, password);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
mail));
msg.setSubject("Attached Demo");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hello " + "\n\n Status of " + sch_Id
+ " is " + jb_status);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(documentName);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
//System.out.println("Mail Sent");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}