我正在开发一个应用程序,我希望使用自己的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");
}
}
}
}
}
答案 0 :(得分:0)
这是检索帐户的代码。
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;
}