【问题标题】:Sending emails with any kind of attachments发送带有任何类型附件的电子邮件
【发布时间】:2019-03-24 05:11:45
【问题描述】:

我想添加一个附件(可以是链接、图像、视频或其他任何内容)以及我通过电子邮件发送的文本。到目前为止,我只能将纯文本作为电子邮件发送。如何添加附件?

这是我在工作线程中发送电子邮件进程的代码:

public class GMailSender 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 msg;

//Progressdialog to show while sending email

//Class Constructor
public GMailSender(Context context, String email, String subject, String msg){
    if (rb1 != null && rad.isChecked()){
        message=s1;
    }else if(rb1 != null && rad1.isChecked())
    {
        message=item;
    }
    //Initializing variables
    this.context = context;
    this.email = s4;
    this.subject = s3;
    this.msg = message;
}

@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
    //Showing a success message
    Toast.makeText(context,"Message Sent",Toast.LENGTH_SHORT).show();
}

@Override
protected Void doInBackground(Void... params) {
    //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.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");

    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(Smscreator.EMAIL, Smscreator.PASSWORD);
                }
            });

    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

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

        //Sending email
        Transport.send(mm);

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

这是发送邮件的代码:

GMailSender sm = new GMailSender(context, s4, s3, message);
    sm.execute();
    Toast.makeText(context, "Email sent :)",
            Toast.LENGTH_SHORT).show();

编辑:按照建议,我已经调查了 [Sending Email in Android using JavaMail API without using the default/built-in app.但是没有任何关于在解决方案中添加附件的文章。

【问题讨论】:

标签: java android jakarta-mail


【解决方案1】:

修复所有这些common JavaMail mistakes。确保您使用的是JavaMail for Android。请参阅how to send a message with an attachment 的 JavaMail 常见问题解答。例如,请参阅sendfile.java sample program

【讨论】:

  • 当我实现这个 sendfile.java 代码时,我的应用程序一直显示白屏,并且从未发送过电子邮件。请帮忙
  • 你使用 sendfile.java 没有改变吗?你是从命令行运行它吗?您是否阅读了源代码以了解如何使用它?如果您想在 Android 应用程序中使用它,您需要提取相关部分并将它们包含在您的应用程序中。如果您已经这样做了,但您的应用程序仍然无法运行,请使用新代码更新您的原始帖子并发布 JavaMail debug output
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 2015-08-28
  • 2013-11-26
  • 2014-11-09
相关资源
最近更新 更多