gpt4 book ai didi

android - 如何在android中发送带有图像作为附件的邮件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:44 24 4
gpt4 key购买 nike

在我的 Android 应用程序中,我需要发送带有图像附件的邮件。我已完成发送邮件。但是如何发送带有图像附件的邮件。我在这里发布发送邮件的代码。请帮助我在以下代码中将图像作为附件发送。

这是代码-

public class MailImageFile extends javax.mail.Authenticator {

public MailImageFile(){}

public void Mail(String user, String pass) {
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("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");

Transport.send(message);

} catch (MessagingException e) {
throw new RuntimeException(e);
}

}

最佳答案

 public class MailImageFile extends javax.mail.Authenticator {

public MailImageFile(){}

public void Mail(String user, String pass) {
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("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
message.setContent(_multipart);
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");

Transport.send(message);

} catch (MessagingException e) {
throw new RuntimeException(e);
}

//Got this solution form here

    private Multipart _multipart; 
_multipart = new MimeMultipart();

public void addAttachment(String filename,String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);

BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);

_multipart.addBodyPart(messageBodyPart2);
}


}

关于android - 如何在android中发送带有图像作为附件的邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15563583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com