在我的应用程序中,我需要向多个收件人发送电子邮件,但我只能发送给一个收件人。我尝试使用分号,但也不起作用。我已经尝试了很多方法,但没有找到解决方案,如果有人知道,请告诉我一下。希望有人能帮我找到解决方法。
提前致谢。
提前致谢。
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
{
}
}