gpt4 book ai didi

java - 在没有 Intent 的情况下在android中发送邮件

转载 作者:行者123 更新时间:2023-11-30 11:22:21 25 4
gpt4 key购买 nike

以下是主类的代码。

public class Main extends Activity {

EditText to, from, message, subject, pass;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button send = (Button) findViewById(R.id.buttonSend);
to = (EditText) findViewById(R.id.editTextTo);
from = (EditText) findViewById(R.id.editTextFrom);
subject = (EditText) findViewById(R.id.editTextSubject);
message = (EditText) findViewById(R.id.editTextMessage);
pass = (EditText) findViewById(R.id.editPassFrom);

final String toString = to.toString();
final String fromString = from.toString();
final String passString = pass.toString();
final String subjString = subject.toString();
final String msgString = message.toString();

send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


try {
GMailSender sender = new GMailSender(fromString, passString);
sender.sendMail(subjString,
msgString,
fromString,
toString);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
to.setText("");
from.setText("");
subject.setText("");
message.setText("");
pass.setText("");
}
});

}
}

这是 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");
}
}
}

我处理了这段代码,但它不起作用。

这是 Logcat 状态:

02-15 01:15:47.132: I/Choreographer(1196): Skipped 30 frames! The application may be doing too much work on its main thread.

最佳答案

只需添加后台邮件库和设置邮件地址,非常简单。

检查这个库,它和你期望的一样,阅读这个库中的自述文件以了解步骤。 Background Mail Library

下载库,解压,它会生成库和演示项目,将这个库导入到您的 Eclipse IDE 或您正在使用 android 的任何 IDE

这里是示例代码,

import com.kristijandraca.backgroundmaillibrary.BackgroundMail;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;


public class MainActivity extends Activity {

Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BackgroundMail bm = new BackgroundMail(MainActivity.this);
bm.setGmailUserName("youremail@gmail.com");
bm.setGmailPassword("yourpassword");
bm.setMailTo("receivermail id");
bm.setFormSubject("Backgroundmail");
bm.setFormBody("TEsting");
bm.setSendingMessage("Sending mail...");
bm.setSendingMessageSuccess("Your message was sent successfully.");
bm.setProcessVisibility(true);
bm.send();

}
}

在项目资源管理器中右键单击您的项目文件夹,然后选择属性转到 android 选项卡,在“is Library”部分添加库,就是这样,添加 Internet 权限。

关于java - 在没有 Intent 的情况下在android中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794192/

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