我是Android开发新手,尝试开发一个使用javax邮件自动发送电子邮件的简单应用。我试过几个SSL和SSL的代码。 TSL(端口号465和587)添加了相应的属性。但它仍然以错误invalid IPv6 type or code from... ,messaging exception, could not connect to smtp host, network unreachable...
返回给我。有人请帮我解决一些问题。
我在下面添加我的代码片段:
MainActivity:
public class MainActivity extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new SendSync().execute();
}
});
}
public class SendSync extends AsyncTask<Void, Void, Void>
{
String error;
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub..
try {
GMailSender sender = new GMailSender("xyz@gmail.com", "pqrst");
sender.sendMail("This is Subject",
"This is Body",
"xyz@gmail.com",
"abcd@gmail.com");
} catch (Exception e) {
error=e.getMessage();
Log.e("SendMail", e.getMessage(), e);
System.out.println("exception "+e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(error!=null)
{
Toast.makeText(getApplicationContext(), "ERROR!!!", Toast.LENGTH_LONG).show();
System.out.println("exception "+error);
}
}
}
Gmailsender类:
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) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
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.starttls.enable", "true");
// props.put("mail.smtp.socketFactory.fallback", "false");
// props.setProperty("mail.smtp.quitwait", "false");
props.setProperty("java.net.preferIPv4Stack", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "false");
props.put("java.net.preferIPv4Stack", "true");
session = Session.getDefaultInstance(props
, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz@gmail.com", "pqrst");// Specify the Username and the PassWord
}
});
//
}
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(user, password);
// }
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
//SmtpAuthenticator authentication = new SmtpAuthenticator();
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 transport = session.getTransport("smtps");
transport.connect(mailhost, 587, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
//Transport.send(message);
}catch(Exception e){
System.out.println("AAAA mailsender 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");
}
}
}
这是我得到的logcat消息:
02-20 11:43:38.804: I/System.out(31376): propertyValue:true
02-20 11:43:38.805: I/System.out(31376): [socket][4] connection smtp.gmail.com/2404:6800:4003:c00::6d:587;LocalPort=54051(0)
02-20 11:43:38.805: I/System.out(31376): [CDS]connect[smtp.gmail.com/2404:6800:4003:c00::6d:587] tm:90
02-20 11:43:38.805: D/Posix(31376): [Posix_connect Debug]Process com.example.mailtest :587
02-20 11:43:38.822: I/dhcpcd(5854): dhcpcd[5855]: invalid IPv6 type or code from fe80::f63e:61ff:fe48:4fd1
02-20 11:43:38.823: I/System.out(31376): [socket][5:54051] exception
02-20 11:43:38.823: I/System.out(31376): [CDS]close[54051]
02-20 11:43:38.823: I/System.out(31376):mailsender exception javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
02-20 11:43:38.823: I/System.out(31376): nested exception is:
02-20 11:43:38.824: I/System.out(31376): java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4003:c00::6d (port 587) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable)
02-20 11:43:38.828: I/NotificationService(492): enqueueToast pkg=com.example.mailtest callback=android.app.ITransientNotification$Stub$Proxy@42bb7948 duration=1