gpt4 book ai didi

java - 使用 servlet 将电子邮件作为附件发送,附件应该只有文件名

转载 作者:行者123 更新时间:2023-12-01 11:37:41 25 4
gpt4 key购买 nike

在此示例中,我尝试使用 gmail smtp 服务器通过邮件发送附件。

Mailer.java

package com.servlet.mail;

import java.io.File;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;

public class Mailer {
public static void send(String to ,String subject ,String msg)
{
System.out.println("in Mailer class");
final String user="sup.ni@gmail.com";
final String psw="XXXXXX";//changes to be made accoordingly


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(user,psw);
}
});


try
{
System.out.println(to+""+subject+""+msg);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
//message.setText(msg);


//Mail sending with attachement



BodyPart msgBodyPart1 = new MimeBodyPart();
msgBodyPart1.setText("This is message body");

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

MimeBodyPart msgBodyPart2 = new MimeBodyPart();


String path = "D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/";
String filename= path+"Note.txt";

System.out.println(path);


message.setHeader("Content-Disposition", "attachement ; filename= \""+filename+"\"");
DataSource src = new FileDataSource(filename);
msgBodyPart2.setDataHandler(new DataHandler(src));
msgBodyPart2.setFileName(path);
multipart.addBodyPart(msgBodyPart2);

message.setContent(multipart);

//SEND MSG
Transport.send(message);
System.out.println("Mail sent successfully");

}
catch(MessagingException e)
{
System.out.println(e);
}
}
}
  1. 要求是我希望附件名称只是文件名。
  2. 在此示例中,当我检查邮件时,附件将具有绝对路径。
  3. 这是我在附件“D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/Note.txt”中收到的文件名
  4. 但我希望附件只是 Note.txt
  5. 请告诉我需要进行哪些更改才能获得所需的输出

最佳答案

您将使用 filename=\".....\" 键值对在 header 中设置文件名。只需将此值更改为您要设置的名称即可。

例如:

 String path = "D:/Self_Learning/Servlets/Servlet_Examples/WebContent/CSS/";
String name = "Note.txt";
String filename= path+name;
message.setHeader("Content-Disposition", "attachement ; filename= \""+name+"\"");

关于java - 使用 servlet 将电子邮件作为附件发送,附件应该只有文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810217/

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