gpt4 book ai didi

JavaMail 和电子邮件帐户的连接

转载 作者:行者123 更新时间:2023-12-01 13:32:17 32 4
gpt4 key购买 nike

我尝试使用 javamail 连接到电子邮件 - 在非 Android 应用程序中,此代码工作正常:

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;
}

}

日志猫:

02-01 13:17:22.863: W/dalvikvm(3436): VFY: unable to resolve static method 4286: 

Ljavax/mail/Session;.getDefaultInstance (Ljava/util/Properties;)Ljavax/mail/Session;
02-01 13:17:22.863: D/dalvikvm(3436): VFY: replacing opcode 0x71 at 0x004b
02-01 13:17:22.893: W/dalvikvm(3436): threadid=13: thread exiting with uncaught exception (group=0x40a71930)
02-01 13:17:23.003: I/Choreographer(3436): Skipped 76 frames! The application may be doing too much work on its main thread.
02-01 13:17:23.213: D/dalvikvm(3436): GC_CONCURRENT freed 150K, 11% free 2612K/2908K, paused 75ms+127ms, total 307ms
02-01 13:17:23.466: E/AndroidRuntime(3436): FATAL EXCEPTION: AsyncTask #3
02-01 13:17:23.466: E/AndroidRuntime(3436): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 13:17:23.466: E/AndroidRuntime(3436): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-01 13:17:23.466: E/AndroidRuntime(3436): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.lang.Thread.run(Thread.java:856)
02-01 13:17:23.466: E/AndroidRuntime(3436): Caused by: java.lang.NoClassDefFoundError: javax.mail.Session
02-01 13:17:23.466: E/AndroidRuntime(3436): at com.example.droidmsg.MainActivity$LoginIn.doInBackground(MainActivity.java:64)
02-01 13:17:23.466: E/AndroidRuntime(3436): at com.example.droidmsg.MainActivity$LoginIn.doInBackground(MainActivity.java:1)
02-01 13:17:23.466: E/AndroidRuntime(3436): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-01 13:17:23.466: E/AndroidRuntime(3436): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-01 13:17:23.466: E/AndroidRuntime(3436): ... 4 more
02-01 13:17:24.123: I/Choreographer(3436): Skipped 34 frames! The application may be doing too much work on its main thread.
02-01 13:17:26.243: I/Choreographer(3436): Skipped 160 frames! The application may be doing too much work on its main thread.

我读到我应该在 asynctask 中执行此操作 - 所以我这样做了,但没有帮助。有人能帮我吗 ?应用程序告诉我“他的应用程序可能在其主线程上做了太多工作”,但我尝试在其他线程中执行此操作?

最佳答案

我在我的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();
}
}

}

关于JavaMail 和电子邮件帐户的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21498636/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com