如何在后台发送电子邮件的Android的? [英] How to send email in background in Android ?
问题描述
有关我的应用程序,我检索到我的信息收件箱/发
。现在,我想送我的电子邮件address.how的检索到的数据可以吗?是否需要任何身份验证呢?此外,我试图下载认证文件(authentication.jar,mail.jar中),但没有成功。
长currentTime的= System.currentTimeMillis的();
长pastThreeHour = currentTime的 - (AlarmManager.INTERVAL_HOUR * 3);
的String [] selectionArgs两个= {+ pastThreeHour,+ currentTime的};
光标光标= contentResolver.query(URI,空,选择,selectionArgs两个,日期降序);
如果(光标=空&安培;!&安培; cursor.getCount()大于0){
而(cursor.moveToNext()){
串号= cursor.getString(cursor.getColumnIndex(地址)); //检查空
字符串日期= cursor.getString(cursor.getColumnIndex(日)); //转换为Date其长
字符串MESSAGE_TEXT = cursor.getString(cursor.getColumnIndex(身体));
字符串类型= cursor.getString(cursor.getColumnIndex(类)); //检查类型,并获得名字
//从这里发送电子邮件
sendSMSEmail(数字,日期,MESSAGE_TEXT,类型);
}
}
cursor.close();
}
是的,这是可以在后台使用下面的code发送电子邮件,发送电子邮件
公共类GMailSender扩展javax.mail.Authenticator
{
私人字符串的邮件主机=smtp.gmail.com;
私人字符串的用户;
私人字符串密码;
私人届会议;
私人多部分_multipart;
静止
{
Security.addProvider(新JSSEProvider());
}
公共GMailSender(用户字符串,字符串密码)
{
this.user =用户;
this.password =密码;
属性道具=新特性();
props.setProperty(mail.transport.protocol,SMTP);
props.setProperty(mail.host,邮件主机);
props.put(mail.smtp.auth,真);
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,假);
props.setProperty(mail.smtp.quitwait,假);
会议=作为Session.getDefaultInstance(道具,这一点);
_multipart =新MimeMultipart的();
}
受保护的PasswordAuthentication的getPasswordAuthentication()
{
返回新的PasswordAuthentication(用户名,密码);
}
市民同步无效的sendmail(字符串学科,身体的字符串,字符串发件人,收件人字符串)抛出异常
{
的MimeMessage消息=新的MimeMessage(会议);
DataHandler的处理程序=新的DataHandler(新ByteArrayDataSource(body.getBytes(),text / plain的));
message.setSender(新网际(发件人));
message.setSubject(学科);
message.setDataHandler(处理);
message.setContent(_multipart);
如果(recipients.indexOf(,)大于0)
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(收件人));
其他
message.setRecipient(Message.RecipientType.TO,新的网际(收件人));
Transport.send(消息);
}
公共无效addAttachment(字符串文件名)抛出异常
{
BodyPart的messageBodyPart =新MimeBodyPart();
数据源源=新FileDataSource(文件名);
messageBodyPart.setDataHandler(新的DataHandler(源));
messageBodyPart.setFileName(文件名);
_multipart.addBodyPart(messageBodyPart);
}
公共类ByteArrayDataSource实现数据源
{
私人byte []的数据;
私人字符串类型;
公共ByteArrayDataSource(byte []的数据,字符串类型)
{
超();
this.data =数据;
this.type =类型;
}
公共ByteArrayDataSource(byte []的数据)
{
超();
this.data =数据;
}
公共无效的setType(字符串类型)
{
this.type =类型;
}
公共字符串的getContentType()
{
如果(类型== NULL)
返回应用程序/八位字节流;
其他
返回类型;
}
公众的InputStream的getInputStream()抛出IOException异常
{
返回新ByteArrayInputStream的(数据);
}
公共字符串的getName()
{
返回ByteArrayDataSource;
}
公众的OutputStream的getOutputStream()抛出IOException异常
{
抛出新的IOException异常(不支持);
}
}
}
和
公开最后一类JSSEProvider扩展供应商
{
私有静态最后长的serialVersionUID = 1L;
公共JSSEProvider()
{
超(HarmonyJSSE,1.0,和谐JSSE提供者);
AccessController.doPrivileged(新java.security.PrivilegedAction<太虚>()
{
公共无效的run()
{
把(SSLContext.TLS
org.apache.harmony.xnet.provider.jsse.SSLContextImpl);
放(Alg.Alias.SSLContext.TLSv1,TLS);
把(KeyManagerFactory.X509
org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl);
把(TrustManagerFactory.X509
org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl);
返回null;
}
});
}
}
您会被要求的activation.jar ,<一个HREF =HTTP://javamail-android.google$c$c.com/files/activation.jar相对=nofollow> additionnal.jar 和放大器; 的mail.jar 文件
For my app, I have retrieved my message inbox/sent
. Now I want to send that retrieved data on my email address.how can it be? Is any authentication required for it? Also I have tried to download the authentication files (authentication.jar,mail.jar) but can't succeeded.
long currentTime = System.currentTimeMillis();
long pastThreeHour = currentTime - (AlarmManager.INTERVAL_HOUR * 3);
String[] selectionArgs = { "" + pastThreeHour, "" + currentTime };
Cursor cursor = contentResolver.query(uri, null, selection, selectionArgs, "date DESC");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex("address")); // check for null
String date = cursor.getString(cursor.getColumnIndex("date")); // convert to date its long
String message_text = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type")); // check type and get names
// send email from here
sendSMSEmail(number, date, message_text, type);
}
}
cursor.close();
}
Yes, it is possible to send email in background use following code, to send email
public class GMailSender extends javax.mail.Authenticator
{
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
static
{
Security.addProvider(new 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);
_multipart = new MimeMultipart();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
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);
message.setContent(_multipart);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public void addAttachment(String filename) throws Exception
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
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");
}
}
}
and
public final class JSSEProvider extends Provider
{
private static final long serialVersionUID = 1L;
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;
}
});
}
}
You would be requiring activation.jar, additionnal.jar & mail.jar files
这篇关于如何在后台发送电子邮件的Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!