关闭

使用SMTP发送邮件

510人阅读 评论(0) 收藏 举报

问题描述:

如何在android上通过SMTP发送邮件?


问题解决:

Google Code上有一个对JavaMail的移植:javamail-android

1.导入三个JAR包

2.创建Mail.java类

可参考:Sending Emails with no intents

import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail extends javax.mail.Authenticator {
	private String _user;
	private String _pass;
	private String[] _to;
	private String _from;
	private String _port;
	private String _sport;
	private String _host;
	private String _subject;
	private String _body;
	private Multipart _multipart;

	public Mail() {
		_host = "smtp.gmail.com"; // default smtp server
		_port = "465"; // default smtp port
		_sport = "465"; // default socketfactory port

		_user = ""; // username
		_pass = ""; // password
		_from = ""; // email sent from
		_subject = ""; // email subject
		_body = ""; // email body
		_multipart = new MimeMultipart();
	}

	public Mail(String user, String pass) {
		this();

		_user = user;
		_pass = pass;
	}

	public boolean send() throws MessagingException, AddressException {
		Properties props = _setProperties();
		Session session = Session.getInstance(props, this);
		if (!_user.equals("") && !_pass.equals("") && _to.length > 0
				&& !_from.equals("") && !_subject.equals("")
				&& !_body.equals("")) {
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(_from));
			InternetAddress[] addressTo = new InternetAddress[_to.length];
			for (int i = 0; i < _to.length; i++) {

				addressTo[i] = new InternetAddress(_to[i]);

			}
			msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

			msg.setSubject(_subject);
			// setup message body
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(_body);
			_multipart.addBodyPart(messageBodyPart);
			// Put parts in message
			msg.setContent(_multipart);
			// send email
			Transport.send(msg);

			return true;
		} else {
			return false;
		}
	}

	@Override
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(_user, _pass);
	}

	private Properties _setProperties() {
		Properties props = new Properties();

		props.put("mail.smtp.host", _host);
		props.put("mail.smtp.auth", "true");

		props.put("mail.smtp.port", _port);
		props.put("mail.smtp.socketFactory.port", _sport);
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		return props;
	}

	public void setBody(String _body) {
		this._body = _body;
	}

	public void setTo(String[] toArr) {
		this._to = toArr;
	}

	public void setFrom(String string) {
		this._from = string;
	}

	public void setSubject(String string) {
		this._subject = string;
	}
}


3.使用Mail.java类发送邮件


Mail m = new Mail("test@gmail.com", "password");//邮箱帐号和密码
String[] toArr = { "to@163.com" };//收件人,可以有多个
m.setTo(toArr);
m.setFrom("test@gmail.com");//发件人

m.setSubject("subject");//邮件主题
m.setBody("body");//邮件内容


测试使用qq邮箱没有问题,但163邮箱失败。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:120093次
    • 积分:1562
    • 等级:
    • 排名:千里之外
    • 原创:43篇
    • 转载:0篇
    • 译文:0篇
    • 评论:20条
    文章分类
    最新评论