Google app engine 使用java mail api从应用程序引擎发送带有图像附件的电子邮件时出现问题
首先,我要感谢大家的回答。这个网站很棒。 其次,我遇到了一个问题,经过几天的搜索,我仍然无法找到答案。我发现很多人都有同样的问题,但没有答案。 我正试图上传一个图像到应用程序引擎上运行的应用程序,并发送一封电子邮件的图像作为附件。我还使用org.apache.commons.fileupload上传图像。 我成功发送了电子邮件,但附件有问题。 我的html表单如下所示:Google app engine 使用java mail api从应用程序引擎发送带有图像附件的电子邮件时出现问题,google-app-engine,servlets,file-upload,email-attachments,Google App Engine,Servlets,File Upload,Email Attachments,首先,我要感谢大家的回答。这个网站很棒。 其次,我遇到了一个问题,经过几天的搜索,我仍然无法找到答案。我发现很多人都有同样的问题,但没有答案。 我正试图上传一个图像到应用程序引擎上运行的应用程序,并发送一封电子邮件的图像作为附件。我还使用org.apache.commons.fileupload上传图像。 我成功发送了电子邮件,但附件有问题。 我的html表单如下所示: <form id="contact" action="/sign" method="post" enctype="mul
<form id="contact" action="/sign" method="post" enctype="multipart/form-data">
<fieldset>
<label>Nume / Prenume</label>
<input type="text" name="nume" />
<label>Telefon</label>
<input type="text" name="telefon" />
<label>E-mail</label>
<input type="text" name="email"/>
</fieldset>
<fieldset>
<label>Textul sesizarii</label>
<textarea name="textulses"></textarea>
<div class="upload-fix-wrap">
<input size="35" class="upload" type="file" name=myFile />
<input type="text" />
<button>Incarca poze</button>
</div>
<button class="send" type="submit">Trimite </button>
</fieldset>
</form>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>com.campiacareiului.CampiaCareiuluiServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
我成功发送了带有以下代码的附件的电子邮件。但我的问题是,当我尝试下载附件或尝试查看附件时,它会说附件已损坏,并且无法识别。我认为问题在于上传图像。要上传图像,我使用org.apache.commons.fileupload。 在这里,我将图像上载到字节数组:
while ((len = stream.read(buffer, 0, buffer.length)) != -1)
{
size+=len;
}
我还可以在“大小”中跟踪上传图像的大小
在这里,我将图像移动到另一个适当尺寸的缓冲区中:
byte[] bufferTemp = new byte[size];
for(int i=0;i<size;i++)
bufferTemp[i]=buffer[i];
byte[]bufferTemp=新字节[size];
对于(int i=0;i我认为示例#1中的问题可能是您正在设置文本正文,然后尝试添加多部分。这是不正确的。消息可以是内容类型:text/plain
或内容类型:multipart/mixed
,但不能两者都是。如果有附件,则需要将文本正文作为多部分之一添加。Somet像这样(未经测试):
我认为示例#1中的问题可能是您设置了一个文本正文,然后试图添加一个多部分。这是不正确的。消息可以是内容类型:text/plain
或内容类型:multipart/mixed
,但不能两者都是。如果有附件,则需要将文本正文作为多部分之一添加。类似于这(未经测试):
我终于让它工作了。问题(当然)是上载图像。我在缓冲区中覆盖了相同的字节,我没有考虑偏移量。正确的方法是:
int len=0;
size=0;
while ((len = stream.read(buffer, size,buffer.length)) != -1)
{
size+=len;
}
我终于让它工作了。问题(当然)是上载图像。我在缓冲区中覆盖了相同的字节,我没有考虑偏移量。正确的方法是:
int len=0;
size=0;
while ((len = stream.read(buffer, size,buffer.length)) != -1)
{
size+=len;
}
谢谢。我已经试过了,但仍然不起作用。在这里,我读取了缓冲区中的图像,并保留了按大小读取的字节数:while((len=stream.read(buffer,0,buffer.length))!=-1){size+=len;问题是缓冲区的大小是8192000,所以我得到了在大小中读取的字节数,我只是将它移动到另一个缓冲区bufferTemp中。我将附件的内容更改为attachment.setContent(bufferTemp,“image/jpeg”);但它仍然不起作用。我建议首先验证是否可以发送“常规”不带附件的电子邮件。然后尝试创建包含多部分/混合和一个或两个文本部分的电子邮件,而不包含图像,以确保您发送电子邮件时不会遇到问题。如果电子邮件正常,则解决图像问题。否则,您需要先解决基本电子邮件发送的问题。我的窍门是在文件内容。如@JJ.所述,您需要将整个文件内容读取到一个数组中。我看到了一个刚刚传入InputStream的示例,因此我认为MimeBodyPart将知道如何处理InputStreams,但它们不知道。因此,我的附件被截断。在手动读取文件内容后,它开始工作。然后我已经试过了,但仍然不起作用。在这里,我读取了缓冲区中的图像,并保留了按大小读取的字节数:while((len=stream.read(buffer,0,buffer.length))!=-1){size+=len;问题是缓冲区的大小是8192000,所以我得到了在大小中读取的字节数,我只是将它移动到另一个缓冲区bufferTemp中。我将附件的内容更改为attachment.setContent(bufferTemp,“image/jpeg”);但它仍然不起作用。我建议首先验证是否可以发送“常规”不带附件的电子邮件。然后尝试创建包含多部分/混合和一个或两个文本部分的电子邮件,而不包含图像,以确保您发送电子邮件时不会遇到问题。如果电子邮件正常,则解决图像问题。否则,您需要先解决基本电子邮件发送的问题。我的窍门是在文件内容。如@JJ.said,您需要将整个文件内容读取到一个数组中。我看到了一个刚刚传入InputStream的示例,因此我认为MimeBodyPart将知道如何处理InputStreams,但它们不知道。因此,我的附件被截断。在手动读取文件内容后,所有内容都开始工作。Ther在你的图像超过8MB的情况下,E仍然是一个问题。考虑使用iouTiLs ToBytErayar()是我所需要的。(我感谢DMAYK)。我喜欢这个位置,如果你的图像超过8MB,这个代码仍然有问题。考虑使用。IouTiLs ToBytErayar()是我所需要的。(谢谢DMAK)。我喜欢这个网站。
byte[] bufferTemp = new byte[size];
for(int i=0;i<size;i++)
bufferTemp[i]=buffer[i];
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.mail.Multipart;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
@SuppressWarnings("serial")
public class CampiaCareiuluiServlet extends HttpServlet {
private String nume=null;
private String telefon=null;
private String email=null;
private String textulses=null;
private String attname=null;
private byte[] buffer = new byte[8192000];
private boolean att=false;
private int size=0;
private static final Logger log =
Logger.getLogger(CampiaCareiuluiServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
ServletFileUpload upload=new ServletFileUpload();
FileItemIterator it = upload.getItemIterator(req);
while(it.hasNext()){
FileItemStream item = it.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
if(name.equals("nume")) nume=Streams.asString(stream);
else if(name.equals("telefon")) telefon=Streams.asString(stream);
else if(name.equals("email")) email=Streams.asString(stream);
else if(name.equals("textulses")) textulses=Streams.asString(stream);
} else {
att=true;
attname=item.getName();
int len;
size=0;
while ((len = stream.read(buffer, 0, buffer.length)) != -1){
size+=len;
}
}
}
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String msgBody = "Nume/Prenume: "+nume+" Telefon: "+telefon+" Mail: "+email+" Textul Sesizarii: "+textulses;
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("ursu.adrian88@gmail.com", "gmail.com Adrian Ursu"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("ursu.adrian88@gmail.com", "Mr. User"));
msg.setSubject("Mail Sesizare Campia Careiului");
if(!att){
msg.setText(msgBody);
}
else{
byte[] bufferTemp = new byte[size];
for(int i=0;i<size;i++)
bufferTemp[i]=buffer[i];
Multipart mp=new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(msgBody, "text/plain");
mp.addBodyPart(textPart);
MimeBodyPart attachment= new MimeBodyPart();
DataSource src = new ByteArrayDataSource
(bufferTemp, "image/jpeg");
attachment.setFileName(attname);
attachment.setDataHandler(new DataHandler
(src));
mp.addBodyPart(attachment);
msg.setContent(mp);
msg.saveChanges();
}
Transport.send(msg);
resp.sendRedirect("/contact.html");
} catch (Exception ex) {
try {
throw new ServletException(ex);
} catch (ServletException e) {
e.printStackTrace();
}
}
}
}
if (!att) {
msg.setText(msgBody);
} else {
//first build and add the text part
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(msgBody, "text/plain");
Multipart mp=new MimeMultipart();
mp.addBodyPart(textPart));
//now read/buffer the image data (?)
byte[] bufferTemp = new byte[size];
for(int i=0;i<=size;i++)
bufferTemp[i]=buffer[i];
// YOU NEED TO FIX THIS!!
}
// now add the attachment part.
// the attachment data must be added all together, not in pieces
MimeBodyPart attachment= new MimeBodyPart();
// YOU NEED TO FIX THIS!!
attachment.setFileName(attname);
attachment.setContent(bufferTemp,"image/jpeg");
mp.addBodyPart(attachment);
msg.setContent(mp);
To: testuser@test.com
From: testuser2@test.com
Date: Aug 19, 2011
Content-Type: multipart/mixed; boundary="aswevb323f23f3f"
This is a message with multiple parts in MIME format.
--aswevb323f23f3f
Content-Type: text/plain
This is the body of the message.
--aswevb323f23f3f
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
ZQs0bWw+CiAgPGhlYWQ+CiAgP49oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZa48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--aswevb323f23f3f
int len=0;
size=0;
while ((len = stream.read(buffer, size,buffer.length)) != -1)
{
size+=len;
}