问题描述
我试图从简单的JavaMail应用程序发送邮件,但是当我尝试在IOS默认邮件应用程序中打开它时,它没有打开(剪辑出现了,但是单击时什么也没有发生)。但是,当我直接从Outlook发送带有附件的电子邮件时,可以将其打开。可能会丢失要打开的特殊标题,或者有其他发送附件的方式吗?
这是我的代码:
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MailTest {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port","587");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
Authenticator auth = new Authenticator() {
@Override protected PasswordAuthentication getpasswordAuthentication() {
return new PasswordAuthentication(MailConstants.AUTH_EMAIL,MailConstants.AUTH_PASSWORD);
}
};
Session session = Session.getInstance(props,auth);
sendEmailWithAttachment(session);
}
private static void sendEmailWithAttachment(Session session) throws MessagingException,IOException {
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type","text/HTML; charset=UTF-8");
msg.addHeader("format","flowed");
msg.addHeader("Content-transfer-encoding","8bit");
msg.setFrom(new InternetAddress("[email protected]","noreply-JD"));
msg.setReplyTo(InternetAddress.parse("[email protected]",false));
msg.setSubject(MailConstants.SUBJECT_MAIL,"UTF-8");
msg.setContent(createContent());
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(MailConstants.RECIPIENT_MAIL,false));
System.out.println("Preparing to send mail...");
Transport.send(msg);
System.out.println("Mail sent to " + MailConstants.RECIPIENT_MAIL);
}
private static MimeMultipart createContent() throws MessagingException,IOException {
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(createTextPart("some test only...",StandardCharsets.UTF_8.name()));
multipart.addBodyPart(createAttachmentPart(new File("documentation.pdf"),StandardCharsets.UTF_8.name()));
return multipart;
}
private static MimeBodyPart createTextPart(String text,String charset) throws MessagingException {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(text,"text/html; charset=" + charset);
return mimeBodyPart;
}
private static MimeBodyPart createAttachmentPart(File file,String charset) throws MessagingException,IOException {
MimeBodyPart attachmentPart = new MimeBodyPart();
DataSource dataSource = new FileDataSource(file);
attachmentPart.setDataHandler(new DataHandler(dataSource));
attachmentPart.setFileName(MimeUtility.encodeText(dataSource.getName(),charset,null));
return attachmentPart;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)