[英]i could not send e-mail in Android using the JavaMail API using Gmail authentication no error detected
我想要使用Gmail身份驗證的JavaMail API在Android中發送電子郵件,它實際上無法正常工作。甚至沒有檢測到錯誤。 我不知道為什么電子郵件沒有發送。
這是我自動收到發送電子郵件的鏈接:
使用JavaMail API在Android中發送電子郵件,而無需使用默認/內置應用
這是主要代碼
package com.example.jawa.pos;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileWriter;
import au.com.bytecode.opencsv.CSVWriter;
/**
* Created by jawa on 10/22/2015.
*/
public class DailyReport_CSV_file extends Activity {
Button csv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GMailSender sender = new GMailSender("username@gmail.com", "password");
sender.sendMail("Cold Store", "Daily Report",
"user@gmail.com",
"user@yahoo.com");
// sender.addAttachment("csvcash.csv","Daily Report");
Toast.makeText(DailyReport_CSV_file.this, "Mail Send Successfully", Toast.LENGTH_SHORT).show();
finish();
}
}
這是我的gmailsender.java文件
package com.example.jawa.pos;
/**
* Created by jawa on 10/23/2015.
*/
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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 java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Provider;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
static {
Security.addProvider(new com.example.jawa.pos.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");
_multipart = new MimeMultipart();
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public void addAttachment(String filename,String subject) throws MessagingException {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);
_multipart.addBodyPart(messageBodyPart2);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) {
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);
message.setContent(_multipart);
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");
}
}
}
我找不到為什么我的消息沒有發送甚至沒有發出錯誤的原因。 誰能給出解決方案?
您是否添加了
<uses-permission android:name="android.permission.INTERNET" />
在你的清單上?
同樣,您不能在主線程上進行網絡操作。 嘗試類似:
private class SendEmail extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... body) {
try{
GMailSender sender = new GMailSender("username@gmail.com", "password");
sender.sendMail("Cold Store", body[0],
"user@gmail.com",
"user@yahoo.com");
}catch (Exception e){
Log.e("Mail Error", e.getMessage());
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
Toast.makeText(this, "Mail Send Successfully", Toast.LENGTH_SHORT).show();
}
}
然后像這樣初始化它:
new SendEmail().execute("Daily Report");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.