我正在开发一个应用程序,我想使用我自己的 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/
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
我最喜欢的Google文档功能之一是它会在我工作时不断自动保存我的文档版本。这意味着即使我在进行关键更改之前忘记在某个点进行保存,也很有可能会自动创建一个保存点。至少,我可以将文档恢复到错误更改之前的状态,并从该点继续工作。对于在MacOS(或UNIX)上运行的Ruby编码器,是否有具有等效功能的工具?例如,一个工具会每隔几分钟自动将Gitcheckin我的本地存储库以获取我正在处理的文件。也许我有点偏执,但这点小保险可以让我在日常工作中安心。 最佳答案 虚拟机有些人可能讨厌我对此的回应,但我在编码时经常使用VIM,它具有自动保存功
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
我想验证一个电子邮件地址是否是PayPal用户。是否有API调用来执行此操作?是否有执行此操作的ruby库?谢谢 最佳答案 GetVerifiedStatus来自PayPal'sAdaptiveAccounts平台会为您做这件事。PayPal没有任何codesamples或SDKs用于Ruby中的自适应帐户,但我确实找到了编写codeforGetVerifiedStatusinRuby的人.您需要更改该代码以检查他们拥有的帐户类型的唯一更改是更改if@xml['accountStatus']!=nilaccount_status
我正在尝试用Ruby(Rails)编写一个正则表达式,以便用户名的字符仅包含数字和字母(也没有空格)。我有这个正则表达式,/^[a-zA-Z0-9]+$/,但它似乎没有用,我在Rails中收到一个错误,说“The如果正则表达式使用多行anchor(^或$),这可能会带来安全风险。您是要使用\A和\z,还是忘记添加:multiline=>true选项?"我的user.rb模型中此实现的完整代码是:classUser我做错了什么以及如何修复此正则表达式,使其仅对数字和字母有效而不对空格有效?谢谢。 最佳答案 简短回答:使用/\A[a-z
我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用rubyonrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_
有没有人得到Logstash在Rails上使用ruby?我的客户告诉我将Logstash用于日志收集器等。我正在使用rubyonrails技术。大部分都快完成了。但要求是将日志记录到logstash中。请让我知道这可能吗? 最佳答案 我为此编写了一个gem-logstasher.它将Rails日志写入一个单独的文件,采用纯json格式,无需任何处理即可由logstash使用。查看我的blog有关如何设置Logstash和Kibana的完整说明 关于ruby-on-rails-Lo
我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位