日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android 谷歌邮箱,Android 使用 SMTP 发送邮件 (Gmail)

發(fā)布時(shí)間:2023/12/10 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 谷歌邮箱,Android 使用 SMTP 发送邮件 (Gmail) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

具體使用方法請(qǐng)看:

http://www.oschina.net/code/snippet_12_983

1.[代碼]GMailSender.java

package org.apache.android.mail;

import javax.activation.DataHandler;

import javax.activation.DataSource;

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 java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

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;

static {

Security.addProvider(new org.apache.harmony.xnet.provider.jsse.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 {

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);

}

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");

}

}

}

總結(jié)

以上是生活随笔為你收集整理的android 谷歌邮箱,Android 使用 SMTP 发送邮件 (Gmail)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。