第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將附件添加到我的郵件發(fā)件人 JavaMail?

如何將附件添加到我的郵件發(fā)件人 JavaMail?

桃花長(zhǎng)相依 2021-11-24 18:34:09
我正在嘗試向我的郵件發(fā)件人添加一個(gè)附件按鈕。我正在使用 JavaMail Mail、activation 和 additionalnal 庫(kù)。我的發(fā)件人被分成 3 個(gè)文件:允許用戶設(shè)置郵件內(nèi)容的MainActivity編輯文本SendMail允許我設(shè)置發(fā)件人,以及從編輯文本到郵件的內(nèi)容并發(fā)送存儲(chǔ)發(fā)送者地址和密碼以及接收者地址等信息的配置這是我的實(shí)際郵件發(fā)件人的代碼,它可以正確發(fā)送郵件但沒有附件。發(fā)送郵件.javapackage 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;}@Overrideprotected void onPreExecute() {    super.onPreExecute();    progressDialog = ProgressDialog.show(context,"Envoi en cours","Veuillez patienter...",false,false);}@Overrideprotected void onPostExecute(Void aVoid) {    super.onPostExecute(aVoid);    progressDialog.dismiss();    Toast.makeText(context,"Message sent",Toast.LENGTH_LONG).show();}@Overrideprotected 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);                }            });
查看完整描述

1 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

我知道了 !我查看了 MimeMessage 文檔并找到了解決方案,您只需將 try(在 SendMail.java 中)的內(nèi)容更改為:


        MimeMessage mimeMessage = new MimeMessage(session);


        MimeMultipart mimeMultipart = new MimeMultipart();



        MimeBodyPart messageBodyPart = new MimeBodyPart();


        messageBodyPart.setContent(message, "text/plain; charset=UTF-8");


        mimeMultipart.addBodyPart(messageBodyPart);




        MimeBodyPart attachmentBodyPart = new MimeBodyPart();


        String filename = "path to your file, exemple : /storage/path.txt" ;

        DataSource source = new FileDataSource(filename);

        attachmentBodyPart.setDataHandler(new DataHandler(source));

        attachmentBodyPart.setFileName(filename);


        mimeMultipart.addBodyPart(attachmentBodyPart);



        mimeMessage.setFrom(new InternetAddress(Config.MAIL_SENDER));


        mimeMessage.addRecipient(Message.RecipientType.TO, new 

        InternetAddress(Config.MAIL_RECEIVER));


        mimeMessage.setSubject(subject);


        mimeMessage.setContent(mimeMultipart);


        Transport.send(mimeMessage);

我還更改了我使用的實(shí)例的名稱。


查看完整回答
反對(duì) 回復(fù) 2021-11-24
  • 1 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)