android发送邮件(包括附件)

来源:互联网 发布:淘宝生e经在哪里订购 编辑:程序博客网 时间:2024/06/05 15:48
    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 0