android发送邮件(包括附件)
2016-02-20 17:24
811 查看
android发送邮件初期遇到的问题:
1.发送到QQ邮箱成为垃圾邮件
2.发送到有些邮箱,没有正文
经过几次试验,终于能够正常发送邮件了。
以下是代码:
public class
MailSender {
/** * send mail
* @param mailInfo Info
*/
public booleansendMail(MailSenderInfomailInfo){
// Determine whether authentication
MyAuthenticator
authenticator;
Properties
pro=mailInfo.getProperties();
if (mailInfo.isValidate()){
// Create a password verifier
authenticator
= new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
else {
return
false;
}
Session
sendMailSession=Session.getDefaultInstance(pro,authenticator);
try {
Message
mailMessage=new
MimeMessage(sendMailSession);
//Create From Address
Address
from=new InternetAddress(mailInfo.getFromAddress());
// Set From Address
mailMessage.setFrom(from);
// Create To Address
Address
to=new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(newDate());
String
mailContent=mailInfo.getContent();
Multipart
mainPart=new
MimeMultipart();
//create body text
MimeBodyPart
body_text=new
MimeBodyPart();
body_text.setText(mailContent);
mainPart.addBodyPart(body_text);
//create Attach File
for
(Stringfilename:mailInfo.getAttachFileNames()){
FileDataSource
fileDataSource = new
FileDataSource(new
File(filename));
DataHandler
dataHandler=new
DataHandler(fileDataSource);
MimeBodyPart
mimeBodyPart=new
MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
try
{
String
fileNameNew=MimeUtility.encodeText(fileDataSource.getName(),"utf-8",null);
mimeBodyPart.setFileName(fileNameNew);
}
catch(UnsupportedEncodingExceptione)
{
e.printStackTrace();
mimeBodyPart.setFileName(fileDataSource.getName());
}
mimeBodyPart.setText(mailInfo.getContent());
mainPart.addBodyPart(mimeBodyPart);
mailMessage.setContent(mainPart);
}
mailMessage.saveChanges();
// send email
Transport.send(mailMessage);
return
true;
} catch(MessagingExceptionex)
{
ex.printStackTrace();
} returnfalse;
}
class MyAuthenticatorextendsAuthenticator
{
String
userName=null;
String
password=null;
public
MyAuthenticator(Stringusername,Stringpassword){
this.userName=username;
this.password=
password;
}
protected
PasswordAuthenticationgetPasswordAuthentication(){
return
newPasswordAuthentication(userName,password);
}
}
}
1.发送到QQ邮箱成为垃圾邮件
2.发送到有些邮箱,没有正文
经过几次试验,终于能够正常发送邮件了。
以下是代码:
public class
MailSender {
/** * send mail
* @param mailInfo Info
*/
public booleansendMail(MailSenderInfomailInfo){
// Determine whether authentication
MyAuthenticator
authenticator;
Properties
pro=mailInfo.getProperties();
if (mailInfo.isValidate()){
// Create a password verifier
authenticator
= new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
else {
return
false;
}
Session
sendMailSession=Session.getDefaultInstance(pro,authenticator);
try {
Message
mailMessage=new
MimeMessage(sendMailSession);
//Create From Address
Address
from=new InternetAddress(mailInfo.getFromAddress());
// Set From Address
mailMessage.setFrom(from);
// Create To Address
Address
to=new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(newDate());
String
mailContent=mailInfo.getContent();
Multipart
mainPart=new
MimeMultipart();
//create body text
MimeBodyPart
body_text=new
MimeBodyPart();
body_text.setText(mailContent);
mainPart.addBodyPart(body_text);
//create Attach File
for
(Stringfilename:mailInfo.getAttachFileNames()){
FileDataSource
fileDataSource = new
FileDataSource(new
File(filename));
DataHandler
dataHandler=new
DataHandler(fileDataSource);
MimeBodyPart
mimeBodyPart=new
MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
try
{
String
fileNameNew=MimeUtility.encodeText(fileDataSource.getName(),"utf-8",null);
mimeBodyPart.setFileName(fileNameNew);
}
catch(UnsupportedEncodingExceptione)
{
e.printStackTrace();
mimeBodyPart.setFileName(fileDataSource.getName());
}
mimeBodyPart.setText(mailInfo.getContent());
mainPart.addBodyPart(mimeBodyPart);
mailMessage.setContent(mainPart);
}
mailMessage.saveChanges();
// send email
Transport.send(mailMessage);
return
true;
} catch(MessagingExceptionex)
{
ex.printStackTrace();
} returnfalse;
}
class MyAuthenticatorextendsAuthenticator
{
String
userName=null;
String
password=null;
public
MyAuthenticator(Stringusername,Stringpassword){
this.userName=username;
this.password=
password;
}
protected
PasswordAuthenticationgetPasswordAuthentication(){
return
newPasswordAuthentication(userName,password);
}
}
}
相关文章推荐
- 垃圾邮件过滤器 python简单实现
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- 使用C#发送带附件的电子邮件的方法的代码示例分析
- C#实现按数据库邮件列表发送邮件的方法
- C#使用自带的email组件发送邮件的方法
- 使用C#编写简单的图形化的可发送附件的邮件客户端程序
- C#.NET采用HTML模板发送电子邮件完整实例
- 使用WordPress发送电子邮件的相关PHP函数用法解析
- 附件名前加网站名
- php简单实现发送带附件的邮件
- 中文附件无法下载的问题分析
- C#发送内置图片html格式邮件的方法
- javascript验证邮件地址和MX记录的方法
- C#实现附件上传和下载功能
- asp+JMAIL实现发送邮件
- C++实现邮件群发的方法
- 使用Java发送带附件的附件的示例