I encountered some problems while sending an email to Gmail email ID which I am using in my application.
我在向我的应用程序中使用的Gmail电子邮件ID发送电子邮件时遇到了一些问题。
I am using JavaMail Api
to send the email.
我正在使用JavaMail Api发送电子邮件。
When I press the button "Send", a Toast message "Sending success" is displayed, in onPostExecute
method, but no new email is sent to Gmail ID,that I am using in program.
当我按下“发送”按钮时,会在onPostExecute方法中显示Toast消息“发送成功”,但没有新的电子邮件发送到我在程序中使用的Gmail ID。
I need help !
我需要帮助 !
My code below:
我的代码如下:
public class GmailSender extends Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new android.readnews.support.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) {
e.printStackTrace();
}
}
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 class SendEmailFragment extends Fragment implements OnClickListener {
Context mContext = null;
Session session = null;
ProgressDialog pDialog = null;
EditText edtYourEmail, edtSubjectEmail, edtBodyEmail;
final String myEmail = "abc@gmail.com";
final String myPass = "abcxyz";
String yourEmail = "";
String Subject = "";
String BodyEmail = "";
GmailSender sender;
public SendEmailFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sendemail_layout, container,
false);
mContext = container.getContext();
ImageButton btnSendMail = (ImageButton) rootView
.findViewById(R.id.buttonSend);
edtYourEmail = (EditText) rootView.findViewById(R.id.tv_MailFromBox);
edtSubjectEmail = (EditText) rootView
.findViewById(R.id.editTextSubject);
edtBodyEmail = (EditText) rootView.findViewById(R.id.editMessage);
btnSendMail.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View view) {
yourEmail = edtYourEmail.getText().toString().trim();
Subject = edtSubjectEmail.getText().toString().trim();
BodyEmail = edtBodyEmail.getText().toString();
pDialog = ProgressDialog.show(mContext, "", "Sending Mail...", true);
SendMail sendmailTask = new SendMail();
sendmailTask.execute();
}
class SendMail extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... arg0) {
try {
sender = new GmailSender(myEmail, myPass);
sender.sendMail(Subject, BodyEmail, myEmail, yourEmail);
return true;
} catch (Exception e) {
Log.i("abc ", "abc " + e.getMessage());
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
if (result == true) {
pDialog.dismiss();
edtYourEmail.setText("");
edtSubjectEmail.setText("");
edtBodyEmail.setText("");
Toast.makeText(mContext, "Sending success", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(mContext, "Sending failure", Toast.LENGTH_SHORT)
.show();
}
super.onPostExecute(result);
}
}
}
Logcat
01-13 15:35:15.697: W/System.err(1394): javax.mail.AuthenticationFailedException
01-13 15:35:15.697: W/System.err(1394): at javax.mail.Service.connect(Service.java:319)
01-13 15:35:15.697: W/System.err(1394): at javax.mail.Service.connect(Service.java:169)
01-13 15:35:15.697: W/System.err(1394): at javax.mail.Service.connect(Service.java:118)
01-13 15:35:15.697: W/System.err(1394): at javax.mail.Transport.send0(Transport.java:188)
01-13 15:35:15.697: W/System.err(1394): at javax.mail.Transport.send(Transport.java:118)
01-13 15:35:15.697: W/System.err(1394): at android.readnews.support.GmailSender.sendMail(GmailSender.java:67)
01-13 15:35:15.697: W/System.err(1394): at android.readnews.main.SendEmailFragment$SendMail.doInBackground(SendEmailFragment.java:93)
01-13 15:35:15.697: W/System.err(1394): at android.readnews.main.SendEmailFragment$SendMail.doInBackground(SendEmailFragment.java:1)
01-13 15:35:15.697: W/System.err(1394): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-13 15:35:15.697: W/System.err(1394): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-13 15:35:15.697: W/System.err(1394): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-13 15:35:15.697: W/System.err(1394): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-13 15:35:15.697: W/System.err(1394): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-13 15:35:15.697: W/System.err(1394): at java.lang.Thread.run(Thread.java:856)
01-13 15:35:15.733: W/InputMethodManagerService(397): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@53787d20 attribute=null, token = android.os.BinderProxy@5375079c
01-13 15:40:25.134: W/ThrottleService(397): unable to find stats for iface rmnet0
1 个解决方案
#1
0
You shouldn't need all the socket factory stuff, start by cleaning up that and your other common mistakes. You don't need your own ByteArrayDataSource since it's included with JavaMail. Then, follow these Gmail instructions and debugging tips. You might need to enable support for less secure apps. OAuth2 support might also be helpful.
你不应该需要所有套接字工厂的东西,首先要清理那些和你的其他常见错误。您不需要自己的ByteArrayDataSource,因为它包含在JavaMail中。然后,请按照这些Gmail说明和调试提示操作。您可能需要为安全性较低的应用启用支持。 OAuth2支持也可能有所帮助。
#1
0
You shouldn't need all the socket factory stuff, start by cleaning up that and your other common mistakes. You don't need your own ByteArrayDataSource since it's included with JavaMail. Then, follow these Gmail instructions and debugging tips. You might need to enable support for less secure apps. OAuth2 support might also be helpful.
你不应该需要所有套接字工厂的东西,首先要清理那些和你的其他常见错误。您不需要自己的ByteArrayDataSource,因为它包含在JavaMail中。然后,请按照这些Gmail说明和调试提示操作。您可能需要为安全性较低的应用启用支持。 OAuth2支持也可能有所帮助。