我整天都遇到问题,但无法找到解决方案。 Plz指南
尝试下面提到的代码,但没有收到任何电子邮件,我发送新的password to user email when
用户在edittext上输入他的电子邮件,并且提交按钮密码被发送到用户输入的电子邮件ID。我正在Forget上做这项工作密码活动。我已阅读许多链接发送电子邮件,但没有用户干预,但没有得到解决方案。请帮助我知道下面的代码有什么问题。我添加了三个罐子Mail.jar,additional.jar,activation.jar和还添加了文件到gradle并仍然面临问题。
package com.example.emailsendnoui;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivitySecond extends Activity {
Button send_btn ;
EditText mail_id_text ;
String mail ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
send_btn = (Button) findViewById(R.id.button1_send);
mail_id_text = (EditText) findViewById(R.id.editText1_mailId);
Log.v("hari","edittext text"+mail_id_text.getText());
send_btn.setOnClickListener(new OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
if(mail_id_text != null) {
StringBuilder str_mail = new StringBuilder() ;
str_mail.append(mail_id_text.getText());
mail = str_mail.toString();
Log.v("hari", "mail:"+mail);
/*StringBuilder str = new StringBuilder() ;
str.append("ErrorName:").append("MalformedURLException :--`");`
str.append("e.getClass").append("--`").append("e.getMessage");*/`
String s = "12345" ;
Log.v("hari", "Response Before:"+s);
emailsend(s);
} else {
Toast.makeText(getApplicationContext(), "Pls enter mail id", Toast.LENGTH_SHORT).show();`
}
}
});
}
private void emailsend(String serverresponse) {
try {
GMailSender sender = new GMailSender("monikacse623@gmail.com", "abc32"); // type ur mail id and password here and next line also
sender.sendMail("Subject : This is Hari Testing ","Body: ServerRespHere: GET new password from DB here:"+serverresponse,"monikacse623@gmail.com",mail);
Toast.makeText(getApplicationContext(), "Email Send Successfully...", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.v("hari","send email"+ e.getMessage(), e);
}
}
}
package com.example.emailsendnoui ;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.Security;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.sql.DataSource;
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, `javax.activation.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");
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection(String theUsername, String `thePassword)`
throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
}
答案 0 :(得分:0)
这里有一个非常明确和有用的音调:Sending Emails without User Intervention (no Intents) in Android
我测试过它,它完美无缺!
答案 1 :(得分:0)
我认为此链接可能会对您有所帮助。 http://androidcodeexamples.blogspot.in/2012/05/android-send-email-via-gmail-actually.html
答案 2 :(得分:0)
你可以试试这个:
选择发送电子邮件:
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
直接发送邮件,您可以点击链接:
sending email without user interaction android
https://www.mindstick.com/Articles/1673/sending-mail-without-user-interaction-in-android
我希望这会对你有所帮助。感谢