首页 > 解决方案 > go-ole 在 html 电子邮件中嵌入图像

问题描述

我正在尝试将图像添加到电子邮件正文。首先,我尝试传递给 base64,它可以工作,但取决于您发送到的域,他们看不到它们。所以我正在尝试使用以下代码。我必须使用 Outlook 发送邮件,因为如果不是通过 Outlook,它不允许我访问服务器。

func send(html, asunto string, archivos []string, para, copia string) error {
    fmt.Println("Enviando correo")
    ole.CoInitialize(0)
    unknown, err := oleutil.CreateObject("Outlook.Application")
    if err != nil {
        log.Print(err)
        return err
    }
    outlook, err := unknown.QueryInterface(ole.IID_IDispatch)
    if err != nil {
        log.Print(err)
        return err
    }
    newmail := oleutil.MustCallMethod(outlook, "CreateItem", "0").ToIDispatch()

    if asunto != "" {
        oleutil.MustPutProperty(newmail, "Subject", asunto)
    }

    if para != "" {
        oleutil.MustPutProperty(newmail, "To", para)
    }

    if copia != "" {
        oleutil.MustPutProperty(newmail, "CC", copia)
    }

    if html != "" {
        oleutil.MustPutProperty(newmail, "HTMLBody", html)
    }

    for _, archivo := range archivos {
        if _, err := os.Stat(archivo); os.IsNotExist(err) {
            fmt.Printf("No se ha podido añadir el archivo%s\n", archivo)
            continue
        }
        newAtt := oleutil.MustGetProperty(newmail, "Attachments").ToIDispatch()
        if strings.HasSuffix(strings.ToLower(archivo), ".png") {
            property := oleutil.MustCallMethod(newAtt, "Add", archivo).ToIDispatch()
            // Here I have tried to set several properties to see if any of them work but I get an error.
            // oleutil.MustPutProperty(property, "ContentId", archivo)
            // oleutil.MustPutProperty(property, "http://schemas.microsoft.com/mapi/proptag/0x3712001F", archivo)
        } else {
            oleutil.MustCallMethod(newAtt, "Add", archivo)
        }
    }

    oleutil.MustCallMethod(newmail, "Send").ToIDispatch()
    fmt.Println("Correo enviado")

}

我添加以下html

const html = `<p>Bolsa en curso Instalaciones:</p>
<img src="cid:%s" alt="foto Bola en curso Instalaciones">

标签: goemail-attachmentsole

解决方案


推荐阅读