@Tyhj
2017-02-24T17:10:23.000000Z
字数 1038
阅读 1536
Android
Android端发送邮件,比较简单,可以用于APP注册时候发送验证码什么的,需要一个用于发送邮件的邮箱,并且要将邮箱的POP3/SMTP服务开启。
package tools;
import android.content.Context;
import com.example.tyhj.tools.R;
//这是必须的jar包
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
/**
* Created by _Tyhj on 2016/7/31.
*
* 有些新申请的账号,不能用来作为发件人
*
*/
public class SendEmail {
private static final String from = "发件邮箱";
//host,就是发送邮件的服务器,如:qq是smtp.qq.com
private static final String host = "smtp.qq.com";
private static final boolean isSSL = true;
//就是发送邮件的服务器端口号
private static final int port = 25;
private static final String username = "发件邮箱";
private static final String password= "密码";
public static void sendEmail(String email2,String content){
//发送邮件
try {
Email email = new SimpleEmail();
//email.setSSLOnConnect(isSSL);
email.setHostName(host);
email.setSmtpPort(port);
email.setAuthentication(username, password);
email.setFrom(from);
email.addTo(email2);
email.setSubject("MSG邮箱验证");
email.setMsg(content);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
//System.out.println("发送完毕!");
}
}