使用smtp发送电子邮件附件

时间:2018-03-07 09:12:18

标签: java android

我试图在zimbra服务器上使用smpt(javamail)发送附件文件(图像),但我得到如下错误:java.io.FileNotFoundException :(没有这样的文件或目录)。我设置文件声明变量的路径与存储在C:目录中的图像,我也尝试设置SD卡的路径,但给我同样的错误(使用uri作为转换为字符串的构造函数的参数)。

如何设置正确的路径。感谢

import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Quota;
import javax.mail.Session;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.webkit.WebChromeClient;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


//Class is extending AsyncTask because this class is going to perform a networking operation
public class SendMail extends AsyncTask<Void, Void, Void> {

    //Declaring Variables
    private Context context;
    private Session session;

    //Information to send email
    private String email;
    private String subject;
    private String message;
    private  String file;



    //Progressdialog to show while sending email
    private ProgressDialog progressDialog;

    //Class Constructor
    public SendMail(Context context, String email, String subject, String message, String file){
        //Initializing variables
        this.context = context;
        this.email = email;
        this.subject = subject;
        this.message = message;
        this.file = file;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Showing progress dialog while sending email
        progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //Dismissing the progress dialog
        progressDialog.dismiss();
        //Showing a success message
        Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(Void... params) {
       // String file="C:/Users/Vullnety/Desktop
     file = "drawable" + "image"+ ".jpg" ;
       // file =context.getResources().getResourceName(R.drawable.image);
        //file ="/storage/emulated/0/photo.jpg";




        //String path = path.toString();
        //String path = otherPath .toString();



        //Creating properties
        Properties props = new Properties();

        //Configuring properties for gmail
        //If you are not using gmail you may need to change the values
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", hostname);
        props.put("mail.smtp.ssl.trust", hostname);

        //props.put("mail.smtp.socketFactory.port", "587");
        // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        //  props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.port", "587");

        //Creating a new session

        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
            }
        };

        Session session = Session.getInstance(props, auth);
/*
        try {
            //Creating MimeMessage object
            MimeMessage mm = new MimeMessage(session);

            //Setting sender address
            mm.setFrom(new InternetAddress(Config.EMAIL));
            //Adding receiver
            mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            //Adding subject
            mm.setSubject(subject);
            //Adding message
            mm.setText(message);

            //Sending email
            Transport.send(mm);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }*/
        try {
            Message msg = new MimeMessage(session);


            msg.setFrom(new InternetAddress(Config.EMAIL));
            InternetAddress[] toAddresses = {new InternetAddress(email)};
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            msg.setSubject("Hello");
           // msg.setText(message);


            // creates message part
            //MimeBodyPart messageBodyPart = new MimeBodyPart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

          // messageBodyPart.setContent(msg,"text/html");

          //  multipart.addBodyPart(bodyPart);

            MimeBodyPart attachPart = new MimeBodyPart();

            DataSource source = new FileDataSource(String.valueOf(file));

            attachPart.setDataHandler(new DataHandler(source));

            //attachPart.setFileName("dada");
           //mimeBodyPart.attachFile(String.valueOf(source));

            multipart.addBodyPart(attachPart);

            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");

           // msg.setContent(multipart);
            //   message.setContent(multipart);
            //messageBodyPart.setContent(message, "text/html");
            msg.setContent(multipart);
            // sends the e-mail
            Transport.send(msg);

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }

}

异常详情:

    03-07 10:19:18.574 5230-5248/thegenuinegourav.email W/System.err: javax.mail.MessagingException: IOException while sending message;
    03-07 10:19:18.574 5230-5248/thegenuinegourav.email W/System.err:   nested exception is:
    03-07 10:19:18.574 5230-5248/thegenuinegourav.email W/System.err:   java.io.FileNotFoundException: drawableimage.jpg (No such file or directory)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.Transport.send0(Transport.java:189)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.Transport.send(Transport.java:118)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at thegenuinegourav.email.SendMail.doInBackground(SendMail.java:193)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at thegenuinegourav.email.SendMail.doInBackground(SendMail.java:43)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.lang.Thread.run(Thread.java:764)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err: Caused by: java.io.FileNotFoundException: drawableimage.jpg (No such file or directory)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.io.FileInputStream.open0(Native Method)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.io.FileInputStream.open(FileInputStream.java:200)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:150)
    03-07 10:19:18.575 5230-5248/thegenuinegourav.email W/System.err:     at javax.activation.FileDataSource.getInputStream(FileDataSource.java:110)
    03-07 10:19:18.576 5230-5248/thegenuinegourav.email W/System.err:     at javax.activation.DataHandler.writeTo(DataHandler.java:318)
    03-07 10:19:18.576 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
    03-07 10:19:18.576 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:874)
    03-07 10:19:18.576 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444)
    03-07 10:19:18.577 5230-5248/thegenuinegourav.email W/System.err:     at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:102)
    03-07 10:19:18.580 5230-5248/thegenuinegourav.email W/System.err:     at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:897)
    03-07 10:19:18.580 5230-5248/thegenuinegourav.email W/System.err:     at javax.activation.DataHandler.writeTo(DataHandler.java:330)
    03-07 10:19:18.580 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
    03-07 10:19:18.580 5230-5248/thegenuinegourav.email W/System.err:     at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
    03-07 10:19:18.581 5230-5248/thegenuinegourav.email W/System.err:     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)

0 个答案:

没有答案