gpt4 book ai didi

java - 无法使用 JavaMailAPI 发送带附件的邮件

转载 作者:太空宇宙 更新时间:2023-11-04 08:22:30 24 4
gpt4 key购买 nike

您好,我想使用 javaMailAPI 发送邮件附件。我的代码是:

public class GmailSender {

private static String HOST = "smtp.gmail.com";
private static String USER = "######";
private static String PASSWORD = "######";
private static String PORT = "465";
private static String FROM = "######";
private static String TO = "######";

private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static String SUBJECT = "Testing JavaMail API";
private static String TEXT = "This is a test message from my java application. Just ignore it";

public static void main(String[] args){
GmailSender.send();
}


public static void makeAttachment(MimeMessage message){

try{

Multipart mp = new MimeMultipart();

//start Code for Attachment

String fileName = "c:/tmp/f.txt";
File f = new File(fileName);


//read file and make byte array
FileInputStream fi = new FileInputStream(f);
byte[] attachmentData = new byte[8000000];

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("hi");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "c:/tmp/f.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);



}catch(Exception e){
System.out.println(">>Exception "+e);
}

}
public static synchronized void send() {
//Use Properties object to set environment properties
Properties props = new Properties();

props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", USER);

props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);

props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

try {

//Obtain the default mail session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);

//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setText(TEXT);
message.setSubject(SUBJECT);
message.setFrom(new InternetAddress(FROM));
message.addRecipient(RecipientType.TO, new InternetAddress(TO));

makeAttachment(message);

message.saveChanges();

//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);

transport.sendMessage(message, message.getAllRecipients());

transport.close();

} catch (Exception e) {
System.out.println(">>GmailSender "+e);

}
}
}

我收到此异常:

    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
boundary="----=_Part_1_21944831.1329056547036"
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
at javax.activation.DataHandler.writeTo(DataHandler.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
at GmailSender.send(GmailSender.java:117)
at GmailSender.main(GmailSender.java:34)
>>GmailSender javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_1_21944831.1329056547036"

最佳答案

当我运行你的代码示例时,它对我有用,这表明环境有所不同。我用谷歌搜索发现this post ,我相信这回答了您的问题。

关于java - 无法使用 JavaMailAPI 发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249524/

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