Go 1.11 ha raggiunto la fine del supporto
e verrà ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment delle applicazioni Go 1.11, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment dei runtime legacy. Le tue applicazioni Go
1.11 continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti
consigliamo di eseguire la migrazione all'ultima versione supportata di Go.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa guida descrive come utilizzare l'API Mail per inviare e ricevere email.
Prima di iniziare
Devi registrare le email del mittente come mittenti autorizzati. Per ulteriori
informazioni, vedi
Chi può inviare email.
Invio della posta
Per inviare posta dalla tua applicazione:
Utilizza il tipo mail.Message per impostare mittente, destinatario, oggetto e corpo
del messaggio.
Invia l'email con la funzione mail.Send.
Il seguente esempio invia un messaggio email all'utente come conferma della creazione di un nuovo account con l'applicazione:
import("bytes""fmt""net/http""google.golang.org/appengine""google.golang.org/appengine/log""google.golang.org/appengine/mail")funcconfirm(whttp.ResponseWriter,r*http.Request){ctx:=appengine.NewContext(r)addr:=r.FormValue("email")url:=createConfirmationURL(r)msg:=&mail.Message{Sender:"Example.com Support <support@example.com>",To:[]string{addr},Subject:"Confirm your registration",Body:fmt.Sprintf(confirmMessage,url),}iferr:=mail.Send(ctx,msg);err!=nil{log.Errorf(ctx,"Couldn't send email: %v",err)}}constconfirmMessage=`Thank you for creating an account!Please confirm your email address by clicking on the link below:%s`
Ricezione della posta
Puoi configurare la tua app per ricevere email in arrivo agli indirizzi nel seguente formato:
anything@appid.appspotmail.com
Per ricevere email:
Attiva la posta in arrivo nel file app.yaml dell'app:
inbound_services:-mail
Configura un gestore per elaborare le email in arrivo, che vengono fornite alla tua app
come dati MIME in una richiesta HTTP POST.
Nella tua app, registra un gestore per il percorso /_ah/mail/:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThis guide provides instructions on how to utilize the Mail API for sending and receiving emails in first-generation runtimes, with guidance for migration options if updating to App Engine Go 1.12+.\u003c/p\u003e\n"],["\u003cp\u003eSending mail involves using the \u003ccode\u003email.Message\u003c/code\u003e type to define the email's details and the \u003ccode\u003email.Send\u003c/code\u003e function to dispatch the message, as demonstrated with a confirmation email example.\u003c/p\u003e\n"],["\u003cp\u003eTo receive mail, you must enable incoming mail services in the \u003ccode\u003eapp.yaml\u003c/code\u003e file and set up a handler to process emails delivered as MIME data via HTTP \u003ccode\u003ePOST\u003c/code\u003e requests to the \u003ccode\u003e/_ah/mail/\u003c/code\u003e path.\u003c/p\u003e\n"],["\u003cp\u003eBefore sending any mail, all sender email addresses must be registered as authorized senders.\u003c/p\u003e\n"],["\u003cp\u003eReceiving email is only possible to addresses formatted as \u003ccode\u003eanything@appid.appspotmail.com\u003c/code\u003e, and this is independent of whether or not the app is on a custom domain.\u003c/p\u003e\n"]]],[],null,["# Sending and Receiving Mail with the Mail API\n\nThis guide describes how to use the Mail API to send and receive mail.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\nBefore you begin\n----------------\n\nYou must register your sender emails as authorized senders. For more\ninformation, see\n[who can send email](/appengine/docs/legacy/standard/python/mail#who_can_send_mail).\n\nSending mail\n------------\n\nTo send mail from your application:\n\n1. Use the `mail.Message` type to set the sender, recipient, subject, and body\n of the message.\n\n2. Send the email with the `mail.Send` function.\n\nThe following example sends an email message to the user as a confirmation that\nthey have created a new account with the application: \n\n import (\n \t\"bytes\"\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"google.golang.org/appengine\"\n \t\"google.golang.org/appengine/log\"\n \t\"google.golang.org/appengine/mail\"\n )\n\n func confirm(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \taddr := r.FormValue(\"email\")\n \turl := createConfirmationURL(r)\n \tmsg := &mail.Message{\n \t\tSender: \"Example.com Support \u003csupport@example.com\u003e\",\n \t\tTo: []string{addr},\n \t\tSubject: \"Confirm your registration\",\n \t\tBody: fmt.Sprintf(confirmMessage, url),\n \t}\n \tif err := mail.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/mail.html#google_golang_org_appengine_mail_Send(ctx, msg); err != nil {\n \t\tlog.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/log.html#google_golang_org_appengine_log_Errorf(ctx, \"Couldn't send email: %v\", err)\n \t}\n }\n\n const confirmMessage = `\n Thank you for creating an account!\n Please confirm your email address by clicking on the link below:\n\n %s\n `\n\nReceiving mail\n--------------\n\nYou can set up your app to receive incoming email at addresses in the following\nformat: \n\n anything@appid.appspotmail.com\n\n| **Note:** Even if your app is deployed on a custom domain, you can't receive email sent to addresses in that domain.\n\nTo receive email:\n\n1. Enable incoming mail in your app's `app.yaml` file:\n\n inbound_services:\n - mail\n\n2. Set up a handler to process incoming emails, which are supplied to your app\n as MIME data in an HTTP `POST` request.\n\n 1. In your app, register a handler to the `/_ah/mail/` path:\n\n func init() {\n \thttp.HandleFunc(\"/_ah/mail/\", incomingMail)\n }\n\n 2. In the handler, read the email's data from the `*http.Request`:\n\n func incomingMail(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \tdefer r.Body.Close()\n \tvar b bytes.Buffer\n \tif _, err := b.ReadFrom(r.Body); err != nil {\n \t\tlog.Errorf(ctx, \"Error reading body: %v\", err)\n \t\treturn\n \t}\n \tlog.Infof(ctx, \"Received mail: %v\", b)\n }\n\n You can use the [`net/mail`](http://golang.org/pkg/net/mail/) package in the\n standard library to parse mail messages."]]