java - 如何将附件添加到我的邮件发件人 JavaMail?
问题描述
我正在尝试向我的邮件发件人添加附件按钮。我正在使用 JavaMail Mail、激活和附加库。我的发件人分为 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);
}
});
try {
MimeMessage mm = new MimeMessage(session);
mm.setFrom(new InternetAddress(Config.EMAIL_SENDER));
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(Config.EMAIL_RECEIVER));
mm.setSubject(subject);
mm.setText(message);
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
MainActivity.java
package com.my_app.attach_mail;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class L3C1 extends AppCompatActivity implements View.OnClickListener {
private EditText objet_siq;
private EditText corps_siq;
private EditText prenom_siq;
private EditText nom_siq;
private EditText telephone_siq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l3_c1);
object = findViewById(R.id.objet_siq);
body = findViewById(R.id.corps_siq);
first_name= findViewById(R.id.prenom_siq);
last_name= findViewById(R.id.nom_siq);
phone= findViewById(R.id.telephone_siq);
Button buttonSend = findViewById(R.id.send_mail_siq);
Button buttonAttachment = findViewById(R.id.add_attachment_siq);
buttonSend.setOnClickListener(this);
buttonAttachment.setOnClickListener(this);
}
private void sendEmail() {
String subject = objet_siq.getText().toString().trim();
String message = "Nom / Prénom : " + prenom_siq.getText().toString().trim() + " " + nom_siq.getText().toString().trim() + "\n" +"Téléphone : " + telephone_siq.getText().toString().trim() + "\n" + "\n" + "Description du problème : " + "\n" + corps_siq.getText().toString().trim() + "\n" + "\n" + "Cordialement, " + prenom_siq.getText().toString().trim() + " " +nom_siq.getText().toString().trim();
SendMail sm_siq = new SendMail(this, subject, message);
sm_siq.execute();
}
@Override
public void onClick(View view) {
sendEmail();
object.setText("");
body.setText("");
first_name.setText("");
last_name.setText("");
phone.setText("");
}
}
解决方案
我知道了 !我查看了 MimeMessage 文档并找到了解决方案,您只需将 try(在 SendMail.java 中)中的内容更改为:
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);
我还更改了我使用的实例的名称。
推荐阅读
- java - ResultSet .getString() 对于 Azure SQL 的大字符串非常慢
- authentication - 是否在 IBM Cloudant 中实现了 CouchDB 代理认证?
- concurrency - Erlang,如何在控制台中打印每个值?
- python - Python 从大型文本文件中读取完整行的块(列值拆分为多行)
- python - ValueError:数学域错误 | Python 中的 log()
- reactjs - 我的 Chart.js 引用 React 状态的选项被忽略
- api - 当我在我的应用程序中调用 API 时,它会向我显示数据作为响应,但在未来的构建器中,它无法向我显示数据
- c++ - 使用类似 strndup 的语义从 char[] 创建 std::string
- c++ - 如何将多个交流调光器添加到单个 arduino?
- c# - 从一侧移动到另一侧时出现坠落物体的问题