如何将附件添加到我的邮件发件人 JavaMail?

我正在尝试向我的邮件发件人添加一个附件按钮。我正在使用 JavaMail Mail、activation 和 additionalnal 库。我的发件人被分成 3 个文件:

  • 允许用户设置邮件内容的MainActivity编辑文本

  • SendMail允许我设置发件人,以及从编辑文本到邮件的内容并发送

  • 存储发送者地址和密码以及接收者地址等信息的配置这是我的实际邮件发件人的代码,它可以正确发送邮件但没有附件。

发送邮件.java

package com.myapp.attch_mail;


import android.annotation.SuppressLint;

import android.app.ProgressDialog;

import android.content.Context;

import android.os.AsyncTask;

import android.widget.Toast;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;



public class SendMail extends AsyncTask<Void,Void,Void> {


private Context context;


private String subject;

private String message;


private ProgressDialog progressDialog;



SendMail(Context context, String subject, String message){


    this.context = context;

    this.subject = subject;

    this.message = message;


}



@Override

protected void onPreExecute() {

    super.onPreExecute();


    progressDialog = ProgressDialog.show(context,"Envoi en cours","Veuillez patienter...",false,false);

}



@Override

protected void onPostExecute(Void aVoid) {

    super.onPostExecute(aVoid);


    progressDialog.dismiss();


    Toast.makeText(context,"Message sent",Toast.LENGTH_LONG).show();

}



@Override

protected Void doInBackground(Void... params) {


    Properties props = new Properties();



    props.put("mail.smtp.host", "smtp.gmail.com");

    props.put("mail.smtp.socketFactory.port", "465");

    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.port", "465");


    Session session = Session.getDefaultInstance(props,

            new javax.mail.Authenticator() {



                protected PasswordAuthentication getPasswordAuthentication() {

                    return new PasswordAuthentication(Config.EMAIL_SENDER, Config.PASSWORD);

                }

            });

桃花长相依
浏览 143回答 1
1回答

Qyouu

我知道了 !我查看了 MimeMessage 文档并找到了解决方案,您只需将 try(在 SendMail.java 中)的内容更改为:&nbsp; &nbsp; &nbsp; &nbsp; MimeMessage mimeMessage = new MimeMessage(session);&nbsp; &nbsp; &nbsp; &nbsp; MimeMultipart mimeMultipart = new MimeMultipart();&nbsp; &nbsp; &nbsp; &nbsp; MimeBodyPart messageBodyPart = new MimeBodyPart();&nbsp; &nbsp; &nbsp; &nbsp; messageBodyPart.setContent(message, "text/plain; charset=UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; mimeMultipart.addBodyPart(messageBodyPart);&nbsp; &nbsp; &nbsp; &nbsp; MimeBodyPart attachmentBodyPart = new MimeBodyPart();&nbsp; &nbsp; &nbsp; &nbsp; String filename = "path to your file, exemple : /storage/path.txt" ;&nbsp; &nbsp; &nbsp; &nbsp; DataSource source = new FileDataSource(filename);&nbsp; &nbsp; &nbsp; &nbsp; attachmentBodyPart.setDataHandler(new DataHandler(source));&nbsp; &nbsp; &nbsp; &nbsp; attachmentBodyPart.setFileName(filename);&nbsp; &nbsp; &nbsp; &nbsp; mimeMultipart.addBodyPart(attachmentBodyPart);&nbsp; &nbsp; &nbsp; &nbsp; mimeMessage.setFrom(new InternetAddress(Config.MAIL_SENDER));&nbsp; &nbsp; &nbsp; &nbsp; mimeMessage.addRecipient(Message.RecipientType.TO, new&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; InternetAddress(Config.MAIL_RECEIVER));&nbsp; &nbsp; &nbsp; &nbsp; mimeMessage.setSubject(subject);&nbsp; &nbsp; &nbsp; &nbsp; mimeMessage.setContent(mimeMultipart);&nbsp; &nbsp; &nbsp; &nbsp; Transport.send(mimeMessage);我还更改了我使用的实例的名称。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java