ANDROID发送带附件的邮件
创始人
2024-10-07 18:34:52
0

要在Android中发送带附件的电子邮件,可以使用JavaMail库。下面是一个示例代码:

  1. 首先,确保在build.gradle文件中添加以下依赖项:
implementation 'com.sun.mail:android-mail:1.6.2'
implementation 'com.sun.mail:android-activation:1.6.2'
  1. 创建一个帮助类用于发送电子邮件。例如,创建一个名为EmailHelper的类。
import android.os.AsyncTask;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailHelper extends AsyncTask {
    private String senderEmail;
    private String senderPassword;
    private String recipientEmail;
    private String subject;
    private String message;
    private String attachmentFilePath;

    public EmailHelper(String senderEmail, String senderPassword, String recipientEmail, String subject, String message, String attachmentFilePath) {
        this.senderEmail = senderEmail;
        this.senderPassword = senderPassword;
        this.recipientEmail = recipientEmail;
        this.subject = subject;
        this.message = message;
        this.attachmentFilePath = attachmentFilePath;
    }

    @Override
    protected Void doInBackground(Void... params) {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(senderEmail, senderPassword);
                    }
                });

        try {
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(senderEmail));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
            mimeMessage.setSubject(subject);

            Multipart multipart = new MimeMultipart();

            // 添加文本内容
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message);
            multipart.addBodyPart(messageBodyPart);

            // 添加附件
            if (attachmentFilePath != null && !attachmentFilePath.isEmpty()) {
                BodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(attachmentFilePath);
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                attachmentBodyPart.setFileName(attachmentFilePath);
                multipart.addBodyPart(attachmentBodyPart);
            }

            mimeMessage.setContent(multipart);

            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
  1. 在需要发送邮件的Activity或Fragment中,可以使用以下代码调用EmailHelper类。
String senderEmail = "your_email@gmail.com";
String senderPassword = "your_password";
String recipientEmail = "recipient_email@gmail.com";
String subject = "Email Subject";
String message = "Email Message";
String attachmentFilePath = "/path/to/attachment/file.pdf";

EmailHelper emailHelper = new EmailHelper(senderEmail, senderPassword, recipientEmail, subject, message, attachmentFilePath);
emailHelper.execute();

请确保将上述代码中的“your_email@gmail.com”替换为实际的发件人电子邮件地址,“your_password”替换为实际的发件人电子邮件密码,“recipient_email@gmail.com”替换为实际的收件人电子邮件地址,以及“/path/to/attachment/file.pdf”替换为实际的附件文件路径。

这样,当调用EmailHelper的execute方法时,它将在后台发送电子邮件。

相关内容

热门资讯

据权威媒体报道!hhpoker... 据权威媒体报道!hhpoker到底可以作必弊吗(透视)嗨动互动辅助软件(好像是有辅助下载)-哔哩哔哩...
受玩家影响!淘宝买wepoke... 受玩家影响!淘宝买wepoker透视有用吗(透视)新西楚有挂吗(好像真的有辅助app)-哔哩哔哩1、...
来临!约局吧德州真的存在透视吗... 来临!约局吧德州真的存在透视吗(透视)土豪联盟怎么开辅助(总是真的有辅助安装)-哔哩哔哩1、下载好土...
记者获悉!wepoker一直输... 记者获悉!wepoker一直输的号能继续打吗(透视)余干六副辅助(本来有辅助神器)-哔哩哔哩1)余干...
今日!智星菠萝可以辅助吗(透视... 今日!智星菠萝可以辅助吗(透视)樱花之盛辅助软件下载(都是存在有辅助修改器)-哔哩哔哩1、上手简单,...
透视代打!pokermaste... 透视代打!pokermaster修改器(透视)微友辅助器免费下载(确实真的是有辅助下载)-哔哩哔哩运...
近期!hhpoker是正规的吗... 近期!hhpoker是正规的吗(透视)全民比鸡bug(其实是有辅助平台)-哔哩哔哩1、首先打开全民比...
据监测!wepoker怎么看底... 据监测!wepoker怎么看底牌(透视)四川家园辅助软件(原来是有辅助工具)-哔哩哔哩1、四川家园辅...
事发当天!德扑之心免费透视(透... 事发当天!德扑之心免费透视(透视)边锋干橙眼辅助(都是存在有辅助app)-哔哩哔哩1、完成边锋干橙眼...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视怎么用(透视)天天福建十三冰修改器(真是真的有辅助软件)-哔哩哔哩1、...