
我正在尝试在Android中创建邮件发送应用程序.
如果我使用:
Intent emailintent = new Intent(androID.content.Intent.ACTION_SEND);这将启动内置的AndroID应用程序;我正在尝试直接发送邮件,而不使用此应用程序.
解决方法:
使用Gmail身份验证使用JavaMail API在AndroID中发送电子邮件.
创建示例项目的步骤:
MailSenderActivity.java:
public class MailSenderActivity extends Activity { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); final button send = (button) this.findVIEwByID(R.ID.send); send.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { try { GMailSender sender = new GMailSender("username@gmail.com", "password"); sender.sendMail("This is Subject", "This is Body", "user@gmail.com", "user@yahoo.com"); } catch (Exception e) { 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 { 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"); } } } JsSEProvIDer.java:
/* * licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache license, Version 2.0 * (the "license"); you may not use this file except in compliance with * the license. You may obtain a copy of the license at * * http://www.apache.org/licenses/liCENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd. * See the license for the specific language governing permissions and * limitations under the license. *//** * @author Alexander Y. Kleymenov * @version $Revision$ */import java.security.AccessController;import java.security.ProvIDer;public final class JsSEProvIDer extends ProvIDer { 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; } }); }}在您的AndroID项目的以下链接中添加3个罐子
> mail.jar
> activation.jar
> additional.jar
Click here – How to add External Jars
并且不要忘记在清单中添加此行:
<uses-permission androID:name="androID.permission.INTERNET" />只需点击下方链接即可更改安全性较低的应用的帐户访问权
https://www.google.com/settings/security/lesssecureapps
运行该项目并检查收件人邮件帐户中的邮件.
干杯!
附:并且不要忘记你不能从androID中的任何Activity做网络 *** 作.
因此,建议使用AsyncTask或IntentService来避免主线程异常上的网络.
Jar文件:@L_502_6@
总结以上是内存溢出为你收集整理的使用JavaMail API在Android中发送电子邮件,而不使用默认/内置应用程序全部内容,希望文章能够帮你解决使用JavaMail API在Android中发送电子邮件,而不使用默认/内置应用程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)