gpt4 book ai didi

java - 如何发送电子邮件?

转载 作者:行者123 更新时间:2023-11-29 03:39:12 24 4
gpt4 key购买 nike

我正在尝试开发一个应用程序来从特定的电子邮件 ID 发送电子邮件。它不会执行并强制关闭,为什么会这样?有什么方法可以发送电子邮件,请查看我的代码。

public class MainActivity extends Activity {
Button send = null;
EditText mailid = null
String emailId = null;
ConnectivityManager conMan = null;
NetworkInfo Info = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button)findViewById(R.id.button1);
mailid = (EditText)findViewById(R.id.editText1);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
conMan = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Info = conMan.getActiveNetworkInfo();
emailId = mailid.getText().toString();

if (Info == null) {
Toast.makeText(getApplicationContext(), "no net connection ", Toast.LENGTH_LONG).show();
} else {
try {
GmailSender sender = new GmailSender("karuna.java@gmail.com", "heohiby");
sender.sendMail("This is Subject",
"This is Body how r u ..",
"karuna.java@gmail.com",
emailId);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
}
});
}

}

这里是 GmailSender

public class GmailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}

public GmailSender(String user, String password) {
this.user = user;
this.password = password;

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
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");

session = Session.getDefaultInstance(props, this);
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}

public synchronized void sendMail(String subject, String body, String sender, String recipients)throws Exception {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {}
}

public class ByteArrayDataSource implements DataSource {
private byte[]data;
private String type;

public ByteArrayDataSource(byte[]data, String type) {
super();
this.data = data;
this.type = type;
}

public ByteArrayDataSource(byte[]data) {
super();
this.data = data;
}

public void setType(String type) {
this.type = type;
}

public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}

public InputStream getInputStream()throws IOException {
return new ByteArrayInputStream(data);
}

public String getName() {
return "ByteArrayDataSource";
}

public OutputStream getOutputStream()throws IOException {
throw new IOException("Not Supported");
}
}
}

和提供者

public final class JSSEProvider extends Provider {

public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction < Void > () {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}

日志文件

12-19 11:07:43.363: E/AndroidRuntime(977):    at dalvik.system.NativeStart.main(Native Method) 
12-19 11:10:55.453: D/AndroidRuntime(1035): Shutting down VM
12-19 11:10:55.453: W/dalvikvm(1035): threadid=1: thread exiting with uncaught exception (group=0x40015578)
12-19 11:10:55.464: E/AndroidRuntime(1035): FATAL EXCEPTION: main
12-19 11:10:55.464: E/AndroidRuntime(1035): java.lang.NoClassDefFoundError: com.yakshna.mail.GmailSender
12-19 11:10:55.464: E/AndroidRuntime(1035): at com.yakshna.mail.MainActivity$1.onClick(MainActivity.java:42)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.view.View.performClick(View.java:2538)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.view.View$PerformClick.run(View.java:9152)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.os.Handler.handleCallback(Handler.java:587)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.os.Looper.loop(Looper.java:123)
12-19 11:10:55.464: E/AndroidRuntime(1035): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-19 11:10:55.464: E/AndroidRuntime(1035): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 11:10:55.464: E/AndroidRuntime(1035): at java.lang.reflect.Method.invoke(Method.java:507)
12-19 11:10:55.464: E/AndroidRuntime(1035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-19 11:10:55.464: E/AndroidRuntime(1035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-19 11:10:55.464: E/AndroidRuntime(1035): at dalvik.system.NativeStart.main(Native Method)
12-19 11:11:02.570: I/Process(1035): Sending signal. PID: 1035 SIG: 9

此处显示错误:

GmailSender sender = new GmailSender("karuna.java@gmail.com", "heohiby");

最佳答案

你现在的问题是你还没有声明 <uses-permission>Android.manifest .

方法getActiveNetworkInfo()需要 ACCESS_NETWORK_STATE许可。

您需要将其放入您的 Android.manifest 中文件

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

关于java - 如何发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13935153/

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