我是android的新手,所以我需要一些快速的帮助。我从here.尝试了这个代码没有错误,但问题出现在send fucntion的transport.send()中,请你纠正它。将非常感谢帮助
my logcat file
01-02 22:59:27.603: D/gralloc_goldfish(755): Emulator without GPU emulation detected.
01-02 22:59:31.013: V/hari(755): edittext texttype your mail here
01-02 22:59:31.723: I/Choreographer(755): Skipped 34 frames! The application may be doing too much work on its main thread.
01-02 22:59:35.463: V/hari(755): mail:007sanketh@gmail.com
01-02 22:59:35.782: D/dalvikvm(755): GC_FOR_ALLOC freed 139K, 9% free 2875K/3132K, paused 29ms, total 41ms
01-02 22:59:36.172: V/sanketh(755): there
01-02 22:59:36.172: V/sanketh(755): username:007sanketh@gmail.com
01-02 22:59:36.182: V/sanketh(755): from add:007sanketh@gmail.com
01-02 22:59:36.182: V/sanketh(755): subject:This is an email sent using my Mail JavaMail wrapper from an Android device.
01-02 22:59:36.182: V/sanketh(755): body:Email body.
01-02 22:59:36.502: D/dalvikvm(755): GC_FOR_ALLOC freed 368K, 14% free 3017K/3504K, paused 27ms, total 31ms
01-02 22:59:36.502: E/MailApp(755): Could not send email
01-02 22:59:36.502: E/MailApp(755): android.os.NetworkOnMainThreadException
01-02 22:59:36.502: E/MailApp(755): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-02 22:59:36.502: E/MailApp(755): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-02 22:59:36.502: E/MailApp(755): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
01-02 22:59:36.502: E/MailApp(755): at javax.mail.Transport.send(Transport.java:117)
01-02 22:59:36.502: E/MailApp(755): at com.example.emailsendnoui.sending.send(sending.java:119)
01-02 22:59:36.502: E/MailApp(755): at com.example.emailsendnoui.MainActivitySecond.send_mail(MainActivitySecond.java:86)
01-02 22:59:36.502: E/MailApp(755): at com.example.emailsendnoui.MainActivitySecond.access$0(MainActivitySecond.java:73)
01-02 22:59:36.502: E/MailApp(755): at com.example.emailsendnoui.MainActivitySecond$1.onClick(MainActivitySecond.java:47)
01-02 22:59:36.502: E/MailApp(755): at android.view.View.performClick(View.java:4240)
01-02 22:59:36.502: E/MailApp(755): at android.view.View$PerformClick.run(View.java:17721)
01-02 22:59:36.502: E/MailApp(755): at android.os.Handler.handleCallback(Handler.java:730)
01-02 22:59:36.502: E/MailApp(755): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 22:59:36.502: E/MailApp(755): at android.os.Looper.loop(Looper.java:137)
01-02 22:59:36.502: E/MailApp(755): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 22:59:36.502: E/MailApp(755): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 22:59:36.502: E/MailApp(755): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 22:59:36.502: E/MailApp(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 22:59:36.502: E/MailApp(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 22:59:36.502: E/MailApp(755): at dalvik.system.NativeStart.main(Native Method)
01-02 22:59:36.512: I/Choreographer(755): Skipped 274 frames! The application may be doing too much work on its main thread.
sending java file
package com.example.emailsendnoui;
import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import android.util.Log;
public class sending extends javax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public sending() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // default smtp port
_sport = "465"; // default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = "testing"; // email subject
_body = "working condition"; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public sending(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
Log.v("sanketh","there");
String sent="no success";
Log.v("sanketh","username:"+_user);
Log.v("sanketh","password:"+_pass);
//Log.v("sanketh","to add:"+_to[0]);
Log.v("sanketh","from add:"+_from);
Log.v("sanketh","subject:"+_subject);
Log.v("sanketh","body:"+_body);
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
//Transport.send(msg,_to);
Transport.send(msg);
Log.v("sanketh","here");
sent="success";
return true;
} else {
return false;
}
}
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);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
// the getters and setters
public String getBody() {
return _body;
}
public void setBody(String _body) {
this._body = _body;
}
public void setTo(String[] toArr) {
this._to = toArr;
}
public void setFrom(String string) {
this._from = string;
}
public void setSubject(String string) {
this._subject = string;
}
// more of the getters and setters …..
}
class which creates and instantiates objects of tht sending class
private void send_mail()
{
sending m = new sending("username", "password");
String[] toArr = {"asdadqwc@gmail.com", "sdasad@sdad.com"};
m.setTo(toArr);
m.setFrom("we2342asd@gmail.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");
try {
//m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Toast.makeText(this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) { enter code here
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
答案 0 :(得分:0)
android.os.NetworkOnMainThreadException
您无法在主线程上执行网络操作。在AsyncTask
或后台线程中执行它们。
答案 1 :(得分:0)
用于发送您正在使用网络的邮件,您在主线程中无法进行的网络操作就是为什么你会得到 android.os.NetworkOnMainThreadException 所以使用 AsyncTask进行网络操作,了解有关使用AsyncTask的更多详细信息以及您正在访问的异常链接How to fix android.os.NetworkOnMainThreadException?
答案 2 :(得分:0)
您遇到的异常是NetworkOnMainThreadException。要解决该问题,您可以在电子邮件功能附近添加此代码:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
了解更多信息:How to fix android.os.NetworkOnMainThreadException?
//修改
这是你使用异步任务来逃避networkonmainthreadexception的方法(示例代码):
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ApplicationListAdappter activity;
private ImageView bmImage;
public DownloadImageTask(ApplicationListAdappter applicationListAdappter, ImageView imageView) {
this.activity = applicationListAdappter;
this.bmImage = imageView;
}
protected void onPreExecute() {
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", "image download error");
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
执行异步任务:
DownloadImageTask loadImages = new DownloadImageTask(ApplicationListAdappter.this, imageView);
loadImages.execute(ap.getIconURL());