GVKun编程网logo

在Unicode文本文件上接收App Engine Python中的电子邮件附件错误

12

在这里,我们将给大家分享关于在Unicode文本文件上接收AppEnginePython中的电子邮件附件错误的知识,同时也会涉及到如何更有效地android-通过电子邮件发送文本文件附件、c#–.NE

在这里,我们将给大家分享关于在Unicode文本文件上接收App Engine Python中的电子邮件附件错误的知识,同时也会涉及到如何更有效地android-通过电子邮件发送文本文件附件、c# – .NET中的电子邮件附件损坏、Django Python中带有附件的电子邮件、Google App Engine(Python)中的电子商务产品类别的内容。

本文目录一览:

在Unicode文本文件上接收App Engine Python中的电子邮件附件错误

在Unicode文本文件上接收App Engine Python中的电子邮件附件错误

我有一些代码可以解析电子邮件并找到附件,然后将其作为db.BlobProperties存储到数据存储中(以后可以将其更改为Blobstore)。问题是,当我发送UTF8编码的文本文件时,它将生成错误。

该代码基本上保存了文件并返回一个键,该键被转换为字符串,然后存储在父电子邮件实体中。如您所见,我先解码文件,然后将其存储为Blob。我已经发送了许多附件,并且此代码适用于除使用Unicode编码的文本以外的所有内容。有一个更好的方法吗?如何处理Unicode文本附件?

代码嗅探

    my_file = []    my_list = []    if hasattr(mail_message, ''attachments''):        file_name = ""        file_blob = ""        for filename, filecontents in mail_message.attachments:            file_name = filename            file_blob = filecontents.decode()            my_file.append(file_name)            my_list.append(str(store_file(self, file_name, file_blob)))

store_file

def store_file(self, file_name, file_blob):    new_file = myblob(file_name = file_name,                       file_blob = file_blob)    return new_file.put()

我尝试file_blob = str(file_blob)在上面使用无济于事。这只会破坏代码,文件永远不会被存储。

Unicode文本文件的日志1

Property file_blob must be convertible to a Blob instance (Blob() argument should be str instance, not unicode)Traceback (most recent call last):  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__    rv = self.router.dispatch(request, response)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher    return route.handler_adapter(request, response)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__    return handler.dispatch()  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch    return self.handle_exception(e, self.app.debug)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch    return method(*args, **kwargs)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 65, in post    self.receive(mail.InboundEmailMessage(self.request.body))  File "/base/data/home/apps/s~ae-baseapp/1.359073377819595139/controllers/InboundHandler.py", line 51, in receive    file_list.append(str(store_file(self, file_name, file_blob)))  File "/base/data/home/apps/s~ae-baseapp/1.359073377819595139/models/MyModel.py", line 63, in store_file    file_blob = file_blob)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 974, in __init__    prop.__set__(self, value)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__    value = self.validate(value)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2780, in validate    (self.name, self.data_type.__name__, err))BadValueError: Property file_blob must be convertible to a Blob instance (Blob() argument should be str instance, not unicode)

记录2,删除filecontents.decode()并将其替换为filecontents。

Property file_blob must be convertible to a Blob instance (Blob() argument should be str instance, not EncodedPayload)Traceback (most recent call last):  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__    rv = self.router.dispatch(request, response)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher    return route.handler_adapter(request, response)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__    return handler.dispatch()  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch    return self.handle_exception(e, self.app.debug)  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch    return method(*args, **kwargs)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 65, in post    self.receive(mail.InboundEmailMessage(self.request.body))  File "/base/data/home/apps/s~ae-baseapp/1.359097282640216691/controllers/InboundHandler.py", line 57, in receive    file_list.append(str(store_file(self, file_name, file_blob)))  File "/base/data/home/apps/s~ae-baseapp/1.359097282640216691/models/MyModel.py", line 64, in store_file    file_blob = file_blob)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 974, in __init__    prop.__set__(self, value)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__    value = self.validate(value)  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2780, in validate    (self.name, self.data_type.__name__, err))BadValueError: Property file_blob must be convertible to a Blob instance (Blob() argument should be str instance, not EncodedPayload)

答案1

小编典典

附件有效负载是EncodedPayload类的实例。附件具有编码和可选字符集。前者是指传输编码,例如base64;后者用于字符编码,例如UTF-8(此处的字符集有点过时和误导性的术语)。该EncodedPayload.decode()方法对传输编码和文本编码都进行解码,正如您所注意到的,如果您只想获取用户附加到其邮件中的原始字节,则该方法不是很有用。

您可以在此处采用多种方法,但是我建议您复制EncodedPayload的逻辑来解码传输编码,如下所示:

if filecontents.encoding and filecontents.encoding.lower() != ''7bit'':  try:    payload = filecontents.payload.decode(filecontents.encoding)  except LookupError:    raise UnknownEncodingError(''Unknown decoding %s.'' % filecontents.encoding)  except (Exception, Error), e:    raise PayloadEncodingError(''Could not decode payload: %s'' % e)else:  payload = filecontents.payload

请注意,如果附件 文本,则在存储附件 需要包括字符编码,否则当您将附件发送回用户时将无法解释它-原始文本可能已使用任何字符编码进行了编码。

同样,如果可以的话,您还应该保存附件的模仿类型,但这在API中的任何地方都没有公开。您可能要考虑完全避免使用IncomingMessage类,而是使用Python的mime消息模块对POST请求的主体进行解码。

android-通过电子邮件发送文本文件附件

android-通过电子邮件发送文本文件附件

我正在尝试附加文本文件以便通过电子邮件发送
但是每当我打开“电子邮件”应用程序时,都会说该文件不存在.

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"musabyahya1005@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(directory+"/data.txt"));

try {
     startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(getBaseContext(), "An Error Happened ", Toast.LENGTH_SHORT).show();
}

解决方法:

如果您尝试通过电子邮件发送文件,请确保它们位于公共可访问位置,并告诉您它是您要发送的文件.使用此代码作为示例.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822").putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@example.com"}).putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail subject").putExtra(android.content.Intent.EXTRA_TEXT, "lalalala");
String targetFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tmp" + File.separator + "test.txt";
Uri attachmentUri = Uri.parse(targetFilePath);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentUri)); 

c# – .NET中的电子邮件附件损坏

c# – .NET中的电子邮件附件损坏

我正在尝试将PDF附件附加到与System.Net.Mail一起发送的电子邮件中.附件添加部分如下所示:
using (MemoryStream pdfStream = new MemoryStream())
{
    pdfStream.Write(pdfData,pdfData.Length);

    Attachment a = new Attachment(pdfStream,string.Format("Receipt_{0}_{1}.pdf",jobId,DateTime.UtcNow.ToString("yyyyMMddHHmm")));

    msg.Attachments.Add(a);

    SmtpClient smtp = new SmtpClient(serverName,port);
    smtp.Credentials = new NetworkCredential(fromEmailName,fromEmailPassword);
    smtp.Send(msg);
}

问题是附件在另一端损坏了.我发现了一些关于这个问题的讨论here,但是该页面上提到的解决方案使用的是System.Web.Mail.MailAttachment,它在.NET 2.0中已经过时了.

我已经尝试更改Attachment类中的TransferEncoding(它取代了MailAttachment),但没有运气.有没有人在.NET 2.0上解决这个问题?

解决方法

您是否尝试在创建附件之前执行pdfStream.Seek(0,SeekOrigin.Begin)以将流重置为开头?

Django Python中带有附件的电子邮件

Django Python中带有附件的电子邮件

如何解决Django Python中带有附件的电子邮件?

我目前正在编写博客,用户可以在其中通过我在网站上实现的公式向我发送帖子请求。我也希望他们向我发送附件,这就是为什么我将表单放入可以加载图像文件的原因。

但是,我从表单收到的电子邮件没有显示图像,而只是显示附件的名称。我需要在sendemail函数中添加一些代码。

有什么建议吗?

''''''

def publicationmemoria(request):
    if request.method == "POST":
        inputEmail = request.POST[''inputEmail'']
        inputNome = request.POST[''inputNome'']
        inputCognome = request.POST[''inputCognome'']
        inputImmagine1 = request.POST[''inputImmagine1'']

        send_mail(
            ''Richiesta di pubblicazione - Memoria'',#subject
            ''Dati persona di contatto:'' + ''\n'' +
            ''Email:'' + '' '' + inputEmail + ''\n'' +
            ''Nome:'' + '' '' + inputNome + ''\n''
            ''Cognome:'' + '' '' + inputCognome + ''\n''
            + inputImmagine1,# message
            inputEmail,# from email
            [''myemail.com''],# to email
        )

        return render(request,''publication-memoria.html'',{''inputEmail'': inputEmail,''inputNome'': inputNome,''inputCognome'': inputCognome,''inputImmagine1'': inputImmagine1,})

    else:
        return render(request,{})

''''''

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Google App Engine(Python)中的电子商务产品类别

Google App Engine(Python)中的电子商务产品类别

我的目标是创建一个电子商务网站,使客户可以在任何产品页面上看到 相关产品 (类似于amazon.com)。

我不知道如何开始这样艰巨的任务。根据我的研究,我的猜测是要执行以下操作:

  1. 创建Category一种:

    class Category(ndb.Model): name = ndb.StringProperty()
  2. 每当创建产品时,就通过祖先关系将其与类别相关联:

    parent_category = ndb.Key("Category", "Books")

    new_product = Product(
    title=”Coding Horrors Book”,
    parent=parent_category).put()

  3. 现在,在每个产品页面上,我可以创建一个查询以返回书籍列表作为 相关产品

我对此方法有一些担忧:

  1. 首先,这似乎不是一个可靠的方法。

  2. 如何指定产品类别之间的层次关系?例如,如果我们有两个产品类别“ AngularJS”,“ VueJS”,我们如何指定这两个类别之间存在某种联系?

答案1

小编典典

首先,要澄清一下,实体祖先不是建立关系所必须的(它有一些缺点),请参见您能帮助我理解nbd密钥类文档还是祖先关系?。和数据存储区中的相关祖先关系

您需要考虑与Google Cloud
Datastore平衡强大和最终的一致性。

其余答案假定未使用任何实体血统。

要将产品与类别(或多个,如果需要,可以使用重复属性)相关联,您可以:

class Product(ndb.Model):     name = ndb.StringProperty()    category = ndb.KeyProperty(kind=''Category'', repeated=True)category = ndb.Key("Category", "Books")new_product = Product(title="Coding Horrors Book",                      category=[category]).put()

这种方法存在可伸缩性问题:如果产品分为多个类别,则更新类别列表的速度将变得越来越慢(整个实体不断增长,每次都需要重新编写),并且如果对属性进行索引,则它很敏感。在爆炸索引问题。

通过将产品类别关系存储为单独的实体可以避免这种情况:

class ProductCategory(ndb.Model):     product = ndb.KeyProperty(kind=''Product'')    category = ndb.KeyProperty(kind=''Category'')

ProductCategory扩展性更好,但是在这种情况下,您将需要查询以确定产品的相关类别实体的键,然后进行键查找以获取这些类别的详细信息,大致如下:

category_keys = ProductCategory.query(ProductCategory.product == product_key) \                               .fetch(keys_only=True, limit=500)if category_keys:    categories = ndb.get_multi(category_keys)    logging.info(''product %s categories: %s'' \                 % (product.title, '',''.join([c.name for c in categories])))

关于在Unicode文本文件上接收App Engine Python中的电子邮件附件错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android-通过电子邮件发送文本文件附件、c# – .NET中的电子邮件附件损坏、Django Python中带有附件的电子邮件、Google App Engine(Python)中的电子商务产品类别等相关知识的信息别忘了在本站进行查找喔。

本文标签: