在Android应用程序中附带邮件发送

时间:2012-03-05 06:39:23

标签: android

提前致谢, 请在Android应用程序中发送邮件,并在附件中附加一些文件。 我想自动发送这封邮件而不使用任何其他邮件屏幕或导航..

再次感谢..

4 个答案:

答案 0 :(得分:1)

基本上,你应该配置一些可以发送邮件的服务。

第二,您需要编写一个应用程序来与可以接收此信息并发送邮件的服务进行交互。

因此,请完整解释您的要求。

答案 1 :(得分:1)

我的问题不明确,但我认为从您的设备发送电子邮件png文件可能会对您有所帮助......

这是代码

  

private static int EMAIL_SUCCESS = 10; //这是响应代码。

File pngFile = new File(Path to your image file);
              Uri pngUri = Uri.fromFile(pngFile);

              String tag = "Your Text here";

              Intent intent = new Intent(android.content.Intent.ACTION_SEND);
              intent.setType("image/png");

              intent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                      "Email Subject Here");

              intent.putExtra(Intent.EXTRA_TEXT, tag);
              intent.putExtra(Intent.EXTRA_STREAM, pngUri);
    startActivityForResult(intent, EMAIL_SUCCESS);

答案 2 :(得分:1)

请参阅以下代码,它将为您提供帮助

Button mail=(Button)findViewById(R.id.Lockbttn);

mail.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        Intent i = new Intent(Intent.ACTION_SEND);
          i.setType("text/plain") ; 
          i.putExtra(Intent.EXTRA_EMAIL,new String[]{"rajubarad@yahoo.com"});
          i.putExtra(Intent.EXTRA_SUBJECT,"subject Title");
          i.putExtra(Intent.EXTRA_TEXT,"Message body description");
          i.putExtra(android.content.Intent.EXTRA_STREAM,Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "mysdfile.txt")));
          startActivity(Intent.createChooser(i,"Select email application"));
    }
});

如果你想发送没有任何中间窗口的邮件。

     // set startActivity with intent only
        startActivity(i);

答案 3 :(得分:1)

试试这段代码..

public class SendAttachment{
                    public static void main(String [] args){ 
             //to address
                    String to="abc@abc.com";//change accordingly
                    //from address
                    final String user="efg@efg.com";//change accordingly
                    final String password="password";//change accordingly 
                     MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
                   mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
                  mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
                  mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
                  mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                  mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
                  CommandMap.setDefaultCommandMap(mc); 
                  //1) get the session object   
                  Properties properties = System.getProperties();
                  properties.put("mail.smtp.port", "465"); 
                  properties.put("mail.smtp.host", "smtp.gmail.com");
                    properties.put("mail.smtp.socketFactory.port", "465");
                    properties.put("mail.smtp.socketFactory.class",
                            "javax.net.ssl.SSLSocketFactory");
                    properties.put("mail.smtp.auth", "true");
                    properties.put("mail.smtp.port", "465");

                  Session session = Session.getDefaultInstance(properties,
                   new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(user,password);
                   }
                  });

                  //2) compose message   
                  try{ 
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(user));
                    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
                    message.setSubject("Hii"); 
                    //3) create MimeBodyPart object and set your message content    
                    BodyPart messageBodyPart1 = new MimeBodyPart();
                    messageBodyPart1.setText("How is This"); 
                    //4) create new MimeBodyPart object and set DataHandler object to this object    
                    MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                //Location of file to be attached
                    String filename = Environment.getExternalStorageDirectory().getPath()+"/R2832.zip";//change accordingly
                    DataSource source = new FileDataSource(filename);
                    messageBodyPart2.setDataHandler(new DataHandler(source));
                    messageBodyPart2.setFileName("Hello"); 
                    //5) create Multipart object and add MimeBodyPart objects to this object    
                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart1);
                    multipart.addBodyPart(messageBodyPart2); 
                    //6) set the multiplart object to the message object
                    message.setContent(multipart ); 
                    //7) send message 
                    Transport.send(message); 
                   System.out.println("MESSAGE SENT....");
                   }catch (MessagingException ex) {ex.printStackTrace();}
                  }
                }