当前位置:编程学习 > wap >>

Android发送邮件体检附件问题!!求大神指导!颜色部分我想添加附件

public boolean sendMail(){
                Properties props = System.getProperties();
                props.put("mail.smtp.host", host);
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.port", "25");  
                props.put("mail.transport.protocol", "smtp");
                Log.v("服务器",host); 
                Log.v("发件人",from); 
                Log.v("收件人",to); 
          
                Session session = Session.getInstance(props, null);
                try {
                        
                        MimeMessage msg = new MimeMessage(session);
                        msg.setFrom(new InternetAddress(from));
                        InternetAddress[] address = { new InternetAddress(to) };//这个地方有问题么????
                        
                        msg.setRecipients(Message.RecipientType.TO, address);
                        msg.setSubject(subject);
                        msg.setText(content);

                        MimeBodyPart mbpFile = new MimeBodyPart();
                        FileDataSource fds = new FileDataSource(filename);
                        try{
                            mbpFile.setDataHandler(new DataHandler(fds));
                        }catch(MessagingException e){
                            e.printStackTrace();
                        }
                        try{
                            mbpFile.setFileName(fds.getName());
                        }catch(MessagingException e){
                            e.printStackTrace();
                        }
                        MimeMultipart mp = new MimeMultipart("mixed");
                                
                        mp.addBodyPart(mbpFile);
                                //    msg.setContent(mp);//加上这句就发不出邮件,不加就能发送邮件
                                        //mbpContent.setText(content);
                                        //mp.addBodyPart(mbpContent);

                
                        msg.setSentDate(new Date());
                        
                        msg.saveChanges();

                        Transport transport = session.getTransport("smtp");
                        try{
                        transport.connect(host, username, password);}
                        catch(Exception e){
                                e.printStackTrace();
                        }
                        
                        transport.sendMessage(msg,msg.getAllRecipients());
                        
                        transport.close();

                }
                catch (MessagingException mex) {
                        mex.printStackTrace();
                        Exception ex = null;
                        if ((ex = mex.getNextException()) != null) {
                                ex.printStackTrace();
                        }
                        return false;
                }
                return true;
        }
另外还有能发出去邮件但是会抛出异常,如下所示
--------------------编程问答--------------------
这是灌水区,兄弟你发错位置了! --------------------编程问答-------------------- 警告就警告呗 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- java!!! --------------------编程问答-------------------- 这不是技术区
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,