I am now using some classes under the packages
com.android.email.mail  as my Mail API,
and now I can receive mail from GMail.
but have some problems to send mail using SMTP.
I dont know to use the API to create MimeMessage actually.
Anyone know about this???

my code to send mail with one text part and one attachment file is as
follows....
(it works if i just send first part only, but fail if I add the second
part...)
appreciate if any ideal...


*************************************************************************************************************************************
import com.android.email.mail.Address;
import com.android.email.mail.MessagingException;
import com.android.email.mail.Message.RecipientType;
import com.android.email.mail.internet.ByteArrayBody;
import com.android.email.mail.internet.MimeBodyPart;
import com.android.email.mail.internet.MimeMessage;
import com.android.email.mail.internet.MimeMultipart;
import com.android.email.mail.transport.SmtpTransport;

....

                try
                {
                        MimeMessage message = new MimeMessage();
                        message.setSentDate(new Date());
                        Address from = new Address("[EMAIL PROTECTED]");
                        Address[] to = new Address[]{new
Address("[EMAIL PROTECTED]")};
                        message.setFrom(from);
                        message.setRecipients(RecipientType.TO, to);
                        message.setSubject("This is a test...");

                        String text = mailText.getText().toString();

                        ByteArrayBody body = new ByteArrayBody(text.getBytes());
                        MimeMultipart rootPart = new MimeMultipart("multipart/
mixed");

                        MimeBodyPart part1 = new MimeBodyPart();

                        part1.setBody(body);

                        MimeBodyPart part2 = new MimeBodyPart();
                        File f = new File("/sdcard/mail/5424/song.mp3");
                        Uri uri = Uri.fromFile(f);
                        ContentResolver contentResolver = getContentResolver();
                        String contentType = contentResolver.getType(uri);

                        if (contentType == null) {
                            contentType = "";
                        }

                        String name = null;
                        int size = 0;

                        Cursor metadataCursor = contentResolver.query(
                                uri,
                                new String[]{OpenableColumns.DISPLAY_NAME,
OpenableColumns.SIZE},
                                null,
                                null,
                                null);
                        if (metadataCursor != null) {
                            try {
                                if (metadataCursor.moveToNext()) {
                                    name = metadataCursor.getString(0);
                                    size = metadataCursor.getInt(1);
                                }
                            } finally {
                                metadataCursor.close();
                            }
                        }

                        if (name == null) {
                            name = uri.getLastPathSegment();
                        }

                        Log.d("SendActivity", "contentType=" + contentType + "
name="  + name + "   size=" + size);

                        ByteArrayOutputStream bos = new ByteArrayOutputStream();

                        bos.reset();
                        try
                        {
                                byte[] tmp = new byte[8192];
                                int cnt = 0;
                                FileInputStream fis = new FileInputStream(f);
                                while( (cnt=fis.read(tmp)) != -1)
                                {
                                        bos.write(tmp, 0, cnt);
                                }
                        } catch (FileNotFoundException e)
                        {
                                e.printStackTrace();
                        } catch (IOException e)
                        {
                                e.printStackTrace();
                        }

                        Log.d("SendActivity", new
Long(bos.toByteArray().length).toString());
                        Log.d("SendActivity", new
Long(Base64.encodeBase64(bos.toByteArray()).length).toString() );

                        ByteArrayBody body2 = new
ByteArrayBody(Base64.encodeBase64(bos.toByteArray()));
                        part2.setBody(body2);
                        part2.setHeader("Content-type", "audio/mpeg; name=" + 
name);
                        part2.setHeader("Content-Transfer-Encoding", "base64");

                        rootPart.addBodyPart(part1);
                        rootPart.addBodyPart(part2);
                        message.setBody(rootPart);

                        SmtpTransport smtp = new SmtpTransport("smtp+ssl+://
myname:[EMAIL PROTECTED]:465");
                        smtp.sendMessage(message);

                } catch (MessagingException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }








On Aug 29, 9:14 pm, elvisw <[EMAIL PROTECTED]> wrote:
> Hi...
>
> It is too hard to arrange the required classes within apache-harmony-
> src and jsse.jar (j2se lib - for SSL) that used at runtime...
> The dependency is quite complicate.
> I read the article 
> already,http://davanum.wordpress.com/2007/12/22/android-send-email-via-gmail-...
> ,
> but still cannot work well.
> Could anyone provide the least extra classes as a package, it will
> help those who work on this topic a lot....
> thanks....
>
> elvis.
>
> On 8月29日, 上午12時36分, "Mark Murphy" <[EMAIL PROTECTED]> wrote:
>
> > >     1. Will android provideMailAPI??
>
> > There does not appear to be a public API formail, such as javax.mail, in
> > the 0.9 SDK.
>
> > That being said, JavaMail worked under M5, if you found the right JAR
> > files. I haven't tried it yet under 0.9, but I should within a week.
>
> > --
> > Mark Murphy (a Commons Guy)http://commonsware.com
> > _The Busy Coder's Guide to Android Development_ Version 1.1 Published!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to