Sending email with attachment through GMailSender?

I have been reading the blogs and have tried numerous implementations but have still failed to get an image attached to an email that I'm sending through GMail using java. I downloaded all the jars and added GMailSender.java, GMailAuthenticator.java, and JSSEProvider.java and I'm able to send regular e-mails just fine. The way I've tried doing it is shown below, with the middle part that's commented out being the part I had hoped would add the image. Below that is the output on the logcat when I try to execute this. Surely I am missing something pretty simple. Could someone point it out to me please? Thanks in advance.

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setDataHandler(handler); /* // Create your new message part BodyPart imgPart = new MimeBodyPart(); // Create a related multi-part to combine the parts MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(imgPart); String fileName = "http://.../sampleBarcode.png"; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); if (classLoader == null) { System.out.println("IT IS NULL AGAIN!!!!"); } } DataSource ds = new URLDataSource(classLoader.getResource(fileName)); imgPart.setDataHandler(new DataHandler(ds)); imgPart.setHeader("Content-ID", "<logoimg_cid>"); multipart.addBodyPart(imgPart); message.setContent(multipart); */ if(recipients.indexOf(',') > 0) { message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); } else message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); Transport.send(message); } catch(Exception e){Log.e("EMAIL_ERROR",e.getMessage(), e);} } null java.lang.NullPointerException at javax.activation.URLDataSource.getContentType(URLDataSource.java:91) at javax.activation.DataHandler.getContentType(DataHandler.java:218) ... ... (plus some more)

-------------Problems Reply------------

I modified the function to accept a File parameter and attach it to the email, here it is

public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

message.setContent(mp);

if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){

}
}

Category:android Views:0 Time:2011-05-15

Related post

  • How to send email with attachment using GmailSender in android 2010-07-22

    I want to know about how to send email with attachment using GmailSender in android. --------------Solutions------------- this is gmailsender,but i want to get flag when username and password enter by user and check from gmail db that user is valid o

  • Send email with attachment from android app using GMailSender fails at Transport.send 2011-12-17

    I am trying to send an email with an image as attachment from my android app. I have followed this post (among many others): Sending email with attachment through GMailSender? So I've done the same thing, and I can send emails, but only without attac

  • Objective C : How to send Email w/ attachment with iPhone OS v2.2.1? 2009-08-11

    Objective C : How to send Email w/ attachment with iPhone OS v2.2.1 ? I don't really want to setup a web server for this purpose as described in http://www.drobnik.com/touch/2009/06/sending-e-mail-attachments/ Many thanks for reading --------------So

  • How to send email with attachment using PHP? 2010-01-08

    I am using following code for sending email with attachment but the proper file is not getting attach with mail. $UnidID = $_COOKIE['UniqueID']; $guid = $_COOKIE['guid']; $target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs"; $fname = getpathmai

  • Sending email with attachment 2010-08-22

    I've a custom form (created with form API) that need send an uploaded file by email. The current form submit handler sends the email without attachment using drupal_mail(). So I'm looking for a solution to properly send email with attachment from Dru

  • Sending Email with attachment In Blackberry 2011-03-10

    Am doing an application in Blackberry in which i need to send an Email with attachment. Went through the sample code which was given in the below link Link : j2me/BlackBerry - How to send Email with Attachment from Application? The problem is when i

  • Send Email with Attachment (Default Mail Client) 2011-04-18

    Right now I've been sending email with Microsoft Outlook with attachment in C#. This works great. I just got a request to achieve the same result with any email client. Is it possible to do this with a WinForm application? If so, any places for me to

  • send email with attachment using php 2011-12-08

    I used this code to send email with attachment using php, but there is something error in the attachment since I receive an email and the attachment appears in the content. before I use the same code and it worked successfully. why??? <?php // sen

  • Sending Email with attachment in network from my Android app 2012-02-16

    I want to create one application which send email with attachment(select file from SD card/gallery). I try to find relevent code but i did'nt find it. Please provide me some code. Here is sample code for sending mail. Thanks in advance. Sendbtn.setOn

  • Send email with attachment in C# using Amazon SES 2012-03-18

    I am using Amazon SES to send email. How to send email with attachment in C# using Amazon SES? Code: AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig(); amConfig.UseSecureStringForAwsSecretKey = false; AmazonSimpleEmailSer

  • How to send email with attachment using mysql query 2012-03-20

    Is there a possible way to send email with attachment using mysql? Currently I can send email using mysql but no attachments. I need to include a docx attachment from hard disk. I create an eml file and send it to IIS (Inetpub) so that it can fetch t

  • Cannot send email or attach files. Email is attempting to send, but keeps on processing. 2012-02-17

    Original title: Cannot send email or attache files. Processing shows that email is attempting to send, but keeps on processing. Get message on attachments we're still getting set up Cannot send email or attache files. Progress circle shows that email

  • As of today, 26 June 2013, I cannot send emails, or attach files to any emails using outlook. 2012-02-20

    As of today, 26 June 2013, I cannot send emails, or attach files to any emails using outlook. --------------Solutions------------- Hi A-Cube, Let me help you with your concern about your issues when sending an email and attaching a file in Outlook.co

  • When sending emails I attach pdf files but they disappear at the other end. 2012-04-14

    When sending emails I attach pdf files but they disappear at the other end. This only happens to a few and I can send the same email via another person and it is OK --------------Solutions------------- You didn't answer Fahimulla's questions. Most im

  • Cannot send email with attachment 2012-04-23

    I am having this problem to, I type out my email via outlook then attached a photo file to send when I click send I get the same message as above. I am able to send a link to view the picture but cannot send email with attachment. --------------Solut

  • Error Sending Email with Attachment 2012-10-03

    HI, I am having this problem since I upgraded to office 2010 Pro Plus when ever i send email with attachment over 1 MB outlook gives error sending email, But actually email is sent and then outlook try again and sends the email again but gives the sa

  • Unable to send email with attachment 2012-12-23

    Original title: Outlook.com webmail Hi , Am not able to send any attachments over the webmail , error: Postmaster error This is an automatically generated Delivery Status Notification. Delivery to the following recipients failed. (removed for securit

  • Cannot send email or attache files. we're still getting set up error message 2014-03-22

    Cannot send email or attache files. Processing shows that email is attempting to send, but keeps on processing. Get message on attachments we're still getting set up --------------Solutions------------- Hello Lanier Chew, Thank you for posting here.

  • Why can't i send emails or attach Items 2014-07-02

    Just tried to send an important email to work but all it says is "Sending" and doesn't actually send, also it wont let me attach files, comes up with "We're still getting set up. Please try again." even with all the fields filled in... anyone got any

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.077 (s). 11 q(s)