gpt4 book ai didi

java - 我需要为多个收件人发送邮件

转载 作者:搜寻专家 更新时间:2023-10-31 19:45:39 25 4
gpt4 key购买 nike

在我的应用程序中,我需要为多个收件人发送邮件,但我只能发送一个收件人。我尝试使用也不起作用的分号。我试过了,但没有得到任何解决方案,如果有人知道这件事,请告诉我。希望有人能帮我找到。

提前致谢。

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/SendMailAttachServlet")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
maxFileSize = 1024 * 1024 * 10, // 10MB
maxRequestSize = 1024 * 1024 * 50) // 50MB
public class SendMailAttachServlet extends HttpServlet {
private String host;
private String port;

public void init() {
// reads SMTP server setting from web.xml file
ServletContext context = getServletContext();
// host = context.getInitParameter("host");
port = context.getInitParameter("port");
}

/**
* handles form submission
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

List<File> uploadedFiles = saveUploadedFiles(request);
host="smtp.gmail.com";
port="587";
String user = request.getParameter("Sender");
String pass = request.getParameter("Password");
String recipient = request.getParameter("recipient");

System.out.println(recipient);
String subject = request.getParameter("subject");
String content = request.getParameter("content");

String resultMessage = "";

try {
EmailUtility.sendEmailWithAttachment(host, port, user, pass,
recipient, subject, content, uploadedFiles);

resultMessage = "The e-mail was sent successfully";
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
} finally {
deleteUploadFiles(uploadedFiles);
request.setAttribute("message", resultMessage);
getServletContext().getRequestDispatcher("/Result.jsp").forward(
request, response);
}
}

/**
* Saves files uploaded from the client and return a list of these files
* which will be attached to the e-mail message.
*/
private List<File> saveUploadedFiles(HttpServletRequest request)
throws IllegalStateException, IOException, ServletException {
List<File> listFiles = new ArrayList<File>();
byte[] buffer = new byte[4096];
int bytesRead = -1;
Collection<Part> multiparts = request.getParts();
if (multiparts.size() > 0) {
for (Part part : request.getParts()) {
// creates a file to be saved
String fileName = extractFileName(part);
if (fileName == null || fileName.equals("")) {
// not attachment part, continue
continue;
}

File saveFile = new File(fileName);
System.out.println("saveFile: " + saveFile.getAbsolutePath());
FileOutputStream outputStream = new FileOutputStream(saveFile);

// saves uploaded file
InputStream inputStream = part.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();

listFiles.add(saveFile);
}
}
return listFiles;
}

/**
* Retrieves file name of a upload part from its HTTP header
*/
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return null;
}

/**
* Deletes all uploaded files, should be called after the e-mail was sent.
*/
private void deleteUploadFiles(List<File> listFiles) {
if (listFiles != null && listFiles.size() > 0) {
for (File aFile : listFiles) {
aFile.delete();
}
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{

}

}

最佳答案

希望对你有帮助

MimeMessage message = new MimeMessage(session);
transport = mailSession.getNewTransport(session);
String to = "mail1@gmail.com , mail2@gmail.com, mail3@gmail.com";
InternetAddress[] parse = InternetAddress.parse(to , true);
message.setRecipients(javax.mail.Message.RecipientType.TO, parse);
transport.send(message);

谢谢拉古.K

关于java - 我需要为多个收件人发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26987594/

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