[英]sending email without user interaction android
I was trying to send email without user interaction. 我试图在没有用户交互的情况下发送电子邮件 every thing worked until the part for sending.
每件事都有效,直到发送部分。
This is the sendemail function in the Gmailsender class that I am using 这是我正在使用的Gmailsender类中的sendemail功能
public void sendEmail() throws MessagingException
{
Log.i("check","start");
String host = "smtp.gmail.com";
String from = "blabla@gmail.com"; //sender email, this is our website email
String pass = "blablabla"; //password of sender email
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Log.i("check","done pops ");
//creating session
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Log.i("check","done sessions ");
InternetAddress toAddress;
toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
Log.i("check","add recipante ");
message.setSubject(subject);
message.setText("This is my app");
Log.i("check","transport");
Transport transport = session.getTransport("smtp");
//connecting..
Log.i("check","connecting");
transport.connect(host, from, pass);
//sending...
Log.i("check","wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Log.i("check","sent");
} }
The log "wanna send" doesn't appear.. 日志“想发送”没有出现..
The main activity that I am creating an object from the gmailSender has the following code: 我从gmailSender创建对象的主要活动包含以下代码:
send = (Button) this.findViewById(R.id.button1);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
GMailSender sender = new GMailSender("blabla@gmail.com", "blablabla");
Log.e("check", "test 1");
sender.sendEmail() ;
Log.e(DEBUG_TAG, "test 2");
} catch (Exception e) {
Log.e(DEBUG_TAG, "test 3"); }
I am getting the "test 3" log which means that the exception happened.. 我得到“测试3”日志,这意味着发生了异常..
The result is that the email is not sent and the app is stuck... I dont know what is the wrong.. 结果是电子邮件没有发送,应用程序被卡住......我不知道有什么不对..
NOTE: I added the internet permission + I added the jar files 注意:我添加了Internet权限+我添加了jar文件
The logcat is logcat是
12-25 12:55:00.774: E/check(1350): test 1
12-25 12:55:00.774: I/check(1350): start
12-25 12:55:00.774: I/check(1350): done pops
12-25 12:55:00.774: I/check(1350): done sessions
12-25 12:55:00.774: I/check(1350): add recipante
12-25 12:55:00.779: I/check(1350): transport
12-25 12:55:00.789: I/check(1350): connecting
12-25 12:55:00.789: E/check(1350): test 3
this is the whole logcat for the check tag : 这是check标签的整个logcat:
12-25 13:20:18.698: E/check(5547): test 1
12-25 13:20:18.698: I/check(5547): start
12-25 13:20:18.698: I/check(5547): done pops
12-25 13:20:18.723: I/check(5547): done sessions
12-25 13:20:18.723: I/check(5547): add recipante
12-25 13:20:18.723: I/check(5547): transport
12-25 13:20:18.733: I/check(5547): connecting
12-25 13:20:18.758: E/check(5547): [Ljava.lang.StackTraceElement;@4193bb00
12-25 13:20:18.758: E/check(5547): android.os.NetworkOnMainThreadException
12-25 13:20:18.758: E/check(5547): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-25 13:20:18.758: E/check(5547): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
12-25 13:20:18.758: E/check(5547): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-25 13:20:18.758: E/check(5547): at java.net.InetAddress.getByName(InetAddress.java:295)
12-25 13:20:18.758: E/check(5547): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
12-25 13:20:18.758: E/check(5547): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
12-25 13:20:18.758: E/check(5547): at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
12-25 13:20:18.758: E/check(5547): at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
12-25 13:20:18.758: E/check(5547): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
12-25 13:20:18.758: E/check(5547): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
12-25 13:20:18.758: E/check(5547): at javax.mail.Service.connect(Service.java:288)
12-25 13:20:18.758: E/check(5547): at javax.mail.Service.connect(Service.java:169)
12-25 13:20:18.758: E/check(5547): at com.example.sendmail.GMailSender.sendEmail(GMailSender.java:100)
12-25 13:20:18.758: E/check(5547): at com.example.sendmail.MailSenderActivity$1.onClick(MailSenderActivity.java:31)
12-25 13:20:18.758: E/check(5547): at android.view.View.performClick(View.java:3627)
12-25 13:20:18.758: E/check(5547): at android.view.View$PerformClick.run(View.java:14329)
12-25 13:20:18.758: E/check(5547): at android.os.Handler.handleCallback(Handler.java:605)
12-25 13:20:18.758: E/check(5547): at android.os.Handler.dispatchMessage(Handler.java:92)
12-25 13:20:18.758: E/check(5547): at android.os.Looper.loop(Looper.java:137)
12-25 13:20:18.758: E/check(5547): at android.app.ActivityThread.main(ActivityThread.java:4511)
12-25 13:20:18.758: E/check(5547): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 13:20:18.758: E/check(5547): at java.lang.reflect.Method.invoke(Method.java:511)
12-25 13:20:18.758: E/check(5547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-25 13:20:18.758: E/check(5547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-25 13:20:18.758: E/check(5547): at dalvik.system.NativeStart.main(Native Method)
Use the JavaMail API , which was written specifically for Android. 使用专为Android编写的JavaMail API 。
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;
public class Mail 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 Mail() {
_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 = ""; // email subject
_body = ""; // 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 Mail(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
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);
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;
}
// more of the getters and setters …..
}
Below is an example of how to use the Mail wrapper, in an Android activity. 下面是如何在Android活动中使用Mail包装器的示例。
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button addImage = (Button) findViewById(R.id.send_email);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Mail m = new Mail("gmailusername@gmail.com", "password");
String[] toArr = {"bla@bla.com", "lala@lala.com"};
m.setTo(toArr);
m.setFrom("wooo@wooo.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(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
});
}
I am getting the "test 3" log which means that the exception happened..
The result is that the email is not sent and the app is stuck... I dont know what is the wrong..
Blind assumption, Just Put your Mail sending code in AsyncTask
. 盲目假设,只需将您的邮件发送到
AsyncTask
。 I think the reason for your Exception is NetworkOnMainThread .. 我认为你的例外的原因是NetworkOnMainThread ..
Also post the full stacktrace of Exception. 同时发布Exception的完整堆栈跟踪 。 So we can further help you..
所以我们可以进一步帮助你..
Update: 更新:
I think this line transport.connect(host, from, pass);
我认为这行
transport.connect(host, from, pass);
from sendEmail()
gives you Exception. 来自
sendEmail()
给你异常。
Word of warning if using "smtp.gmail.com" as the default smtp server. 如果使用“smtp.gmail.com”作为默认的smtp服务器,则发出警告。
Google will force you to change your linked email account password frequently due to their over zealous "suspicious activity" polices. 由于他们过于热心的“可疑活动”政策,Google会强制您经常更改关联的电子邮件帐户密码。 In essence it treats repeated smtp requests from different countries within a short time frame as "suspicious activity".
实质上,它在短时间内将来自不同国家的重复smtp请求视为“可疑活动”。 As they assume you (the email account holder) can only be in one country at a time.
因为他们认为您(电子邮件帐户持有人)一次只能在一个国家/地区。
When google systems detect "suspicious activity" it will prevent further emails until you change the password. 当谷歌系统检测到“可疑活动”时,它将阻止更多电子邮件,直到您更改密码。 As you will have hard coded the password into the app you have to re-release the app each time this happens, not ideal.
由于您将密码硬编码到应用程序中,因此每次发生这种情况时都必须重新发布应用程序,这并不理想。 This happened 3 times in a week to me, I even stored the password on another server and dynamically fetched the password each time google forced me to change it.
这对我来说每周发生3次,我甚至将密码存储在另一台服务器上,并且每次google强制我更改密码时动态获取密码。
So I recommend using one of the many free smtp providers instead of "smtp.gmail.com" to avoid this security problem. 因此,我建议使用众多免费smtp提供程序之一而不是“smtp.gmail.com”来避免此安全问题。 Use the same code but change "smtp.gmail.com" to your new smtp forwarding host.
使用相同的代码,但将“smtp.gmail.com”更改为新的smtp转发主机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.