android发邮件;提供了两种方式,只带的gmail发送;跟jmail发送;
javamail下载地址
http://code.google.com/p/javamail-android/downloads/list
1.自带的gmail发送,特点是方便,几句代码就搞定,但不够灵活,有局限性;
下面是使用gamil发送方式;
String[] recipients = {"目标邮箱地址1", "目标邮箱地址2(可以多个邮箱地址)"};
String subject = "TestGmail";
String text = "TestGmail邮件内容";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(intent);
2.jamil发送,代码量多点,不受gamil限制;稍复杂点,灵活性高;
步骤;
1.构建邮件消息
2.建立服务器的会话
3.发送邮件
邮件消息实体类
package com.hua.guard.mail;
import java.util.Properties;
/**
* 发送邮件信息
* @author Alan
*/
public class MailSenderInfo
{
//1. 发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
//2.是否验证
private boolean validate = false;
//3.用户账户
private String userName;
private String password;
//4.邮件内容信息
private String fromAddress;
private String toAddress;
private String subject;
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
/**
* * 获得邮件会话属性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost()
{
return mailServerHost;
}
public void setMailServerHost(String mailServerHost)
{
this.mailServerHost = mailServerHost;
}
public String getMailServerPort()
{
return mailServerPort;
}
public void setMailServerPort(String mailServerPort)
{
this.mailServerPort = mailServerPort;
}
public boolean isValidate()
{
return validate;
}
public void setValidate(boolean validate)
{
this.validate = validate;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getFromAddress()
{
return fromAddress;
}
public void setFromAddress(String fromAddress)
{
this.fromAddress = fromAddress;
}
public String getToAddress()
{
return toAddress;
}
public void setToAddress(String toAddress)
{
this.toAddress = toAddress;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
public String[] getAttachFileNames()
{
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames)
{
this.attachFileNames = attachFileNames;
}
}
2.组装邮件信息
package com.hua.guard.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;
/**
* 发送邮件 可以方式附件 html 格式 文本格式
* @author Alan
*/
public class MultiMailsender
{
/**
* 发送文本类型邮件
* @param mailInfo
* @return
*/
public boolean sendTextMail(MailSenderInfo mailInfo)
{
MyAuthenticator myAuthenticator = null;
// 是否验证
if (mailInfo.isValidate())
{
myAuthenticator = new MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
Properties pro = mailInfo.getProperties();
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,
myAuthenticator);
// 根据session创建一个邮件消息
try
{
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* html文件格式的邮件
* @param mailInfo
* @return
*/
public boolean sendHtmlMail(MailSenderInfo mailInfo){
MyAuthenticator myAuthenticator = null;
// 是否验证
if (mailInfo.isValidate())
{
myAuthenticator = new MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
Properties pro = mailInfo.getProperties();
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,
myAuthenticator);
// 根据session创建一个邮件消息
try
{
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
// 设置邮件内容
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(mailContent, "text/html;charset=GBK");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
//发送
Transport.send(mailMessage);
}catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
}
3.进行发送
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import com.hua.util.Configer;
import com.hua.util.Constant;
/**
* 对外公开的方法 进行发送邮件
* @author Alan
*
*/
public class SendEmailHtmLocation
{
/**
* 发送html格式邮件
*/
public static boolean sendHtmlMail(File file){
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(Constant.emailServicHost);
mailInfo.setMailServerPort(Constant.emailPort);
mailInfo.setValidate(true);
mailInfo.setUserName(Constant.emailUserName);
mailInfo.setPassword(Constant.emailPwd);
mailInfo.setFromAddress(Constant.emailUserName);
mailInfo.setToAddress(Configer.SMS_EMAIL_ADDRESS);
mailInfo.setSubject("test信息");
BufferedReader br = null;
StringBuffer sb = null;
try{
//将文件写成string
FileInputStream fileInputStream = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(br!=null){
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
}
mailInfo.setContent(sb.toString());
MultiMailsender sender = new MultiMailsender();
sender.sendHtmlMail(mailInfo);
return true;
}
}