gpt4 book ai didi

android - 检索主帐户名以在android 2.1中发送邮件

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:06 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我想使用我自己的 API 将邮件发送到多个接收点。我已经实现了向他们发送邮件,但为此我硬编码了我的电子邮件 ID 和密码。现在我想使用用户内置的主帐户从他自己的 Android 手机发送邮件。我发现了类似 This 的东西.但我不知道如何使用它,它也不会出现在 if 语句中。有没有其他方法可以实现这一目标。请解决我的问题。

提前致谢

AutoMailActivity.java

public class AutoMailActivity  extends Activity 
{
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
AppLogger.LogError("Reached to Step1");
final Button send = (Button) this.findViewById(R.id.send);
final EditText address;
final EditText subject;
final EditText emailtext;

address = (EditText) findViewById(R.id.emailaddress);

subject = (EditText) findViewById(R.id.emailsubject);

emailtext = (EditText) findViewById(R.id.emailtext);

send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub
final String email1 = emailtext.getText().toString();
final String sub = subject.getText().toString();
final String adrs = address.getText().toString();

try {
AppLogger.LogError("Reached to Step2");


GMailSender sender = new GMailSender("example@gmail.com","mypassword");

AppLogger.LogError("Reached to Step3");
sender.sendMail(sub,email1,"example@gmail.com", adrs);
AppLogger.LogError("Reached to Step4");
} catch (Exception e) {
AppLogger.LogError("Reached to Step5");
Log.e("SendMail", e.getMessage(), e);
}

}
});

现在GmailSender.java

public class GMailSender extends javax.mail.Authenticator
{
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;

static {
AppLogger.LogError("Reached to Step1.1");
Security.addProvider(new com.provider.JSSEProvider());
}

public GMailSender(String user, String password) {
AppLogger.LogError("Reached to Step1.2");
this.user = user;
this.password = password;

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
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");

AppLogger.LogError("Reached to Step1.3");
session = Session.getDefaultInstance(props, this);
AppLogger.LogError("Reached to Step1.4");
}

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

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
AppLogger.LogError("Reached to Step1.5");
MimeMessage message = new MimeMessage(session);
AppLogger.LogError("Reached to Step1.6");
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients));
AppLogger.LogError("Reached to Step1.7");
message.setSubject(subject);
message.setDataHandler(handler);
AppLogger.LogError("Reached to Step1.8");



if (recipients.indexOf(',') > 0)
{
AppLogger.LogError("Reached to Step1.9");
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
Transport.send(message);
AppLogger.LogError("Reached to Step2.1");
}
else
{
AppLogger.LogError("Reached to Step2.2");
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
AppLogger.LogError("Reached to Step2.3");
}

AppLogger.LogError("Reached to Step2.4");
}catch(Exception e)
{
AppLogger.LogError(e.getMessage());
}




}


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

}
}

最佳答案

这是检索帐户的代码。

    private String getAccounts(Context context) {
String accountNames = "";
try {
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccounts();
for (Account account : accounts) {
accountNames += "," + account.name;
}
if (accountNames.length() > 0)
accountNames = accountNames.substring(1);
} catch (Exception e) {
Log.e(TAG, "Exception getAccounts : " + e);
}
return accountNames;
}

关于android - 检索主帐户名以在android 2.1中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8076051/

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