【问题标题】:Error when sending email through outlook in vb?在VB中通过Outlook发送电子邮件时出错?
【发布时间】:2012-03-05 17:19:18
【问题描述】:

未处理的类型异常 'System.Runtime.InteropServices.COMException' 发生在 我的程序.exe

附加信息:操作中止(HRESULT 异常: 0x80004004 (E_ABORT))

以下代码是导致错误的原因:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim AppOutlook As New outlook.Application
        Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
        AppOutlook = CreateObject("Outlook.Application")
        Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
        Recipents.Add("oliverbroomhall1712@hotmail.co.uk")
        OutlookMessage.Subject = "Sending through Outlook"
        OutlookMessage.Body = "Testing outlook Mail"
        OutlookMessage.Send()
        OutlookMessage = Nothing
        AppOutlook = Nothing

    End Sub

在第 7 行发现错误:

Dim Recipents As outlook.Recipients = OutlookMes​​sage.Recipients

如果不是太复杂,有没有办法在没有前景的情况下做到这一点?因为当用户没有安装 Outlook 时会发生什么?如果有人可以帮助我,我需要一种从我的应用程序发送电子邮件的方法:)

【问题讨论】:

    标签: vb.net email error-handling outlook send


    【解决方案1】:

    我从未尝试过做你正在做的事情,但你的台词不应该:

       Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
       AppOutlook = CreateObject("Outlook.Application")
    

    被逆转?所以你的 OutlookMes​​sage 不是基于那个新实例?当然,是的,如果他们没有安装 Outlook,您会遇到这个问题。

    我认为,如果无法从您的用户那里获取电子邮件设置信息,那么以一种始终有效的方式发送电子邮件可能会有些困难。考虑到端口 25 阻塞和 SMTP 过滤等。即使您使用控制面板中列出的用户输入的邮件设置,现在有这么多人使用基于 Web 的电子邮件,我无法想象这会起作用。我想您可以使用自己的电子邮件帐户,您知道该帐户可以发送。

        Dim Smtp As New SmtpClient()
        Smtp.Credentials = New Net.NetworkCredential("user@gmail.com", "password")
        Smtp.EnableSsl = True
        Smtp.Port = 587
        Smtp.Host = "smtp.gmail.com"
        Dim Email As New MailMessage()
        Email.From = New MailAddress("user@gmail.com")
        Email.To.Add("oliverbroomhall1712@gmail.com")
        Email.Subject = "Test Mail"
        Email.Body = "SMTP server test"
        Smtp.Send(Email)
    

    但这对我来说有点不对劲。我想我可能会尝试其中一种解决方案,并让用户有机会为您提供一些设置,如果他们不是这样的话。

    【讨论】:

    • 将电子邮件变暗为新 MailMessage()
    • Email.From = New MailAddress("user@gmail.com")
    • 忽略我的其他 cmets :/ Dim Recipents As outlook.Recipients = OutlookMes​​sage.Recipients 似乎是问题而不是 Outlook 消息
    • 我举的例子都在 Net.Mail 命名空间中。您可以在类文件的顶部添加一个导入行以消除错误: Imports System.Net.Mail
    • 至于为什么 Outlook 的东西不起作用,我不确定。我在我的系统上测试了你的代码,它可以工作。您可能想尝试将代码包装在 try catch 中,并可能从文件中的异常输出一些附加信息。
    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2011-09-14
    • 2012-03-03
    • 1970-01-01
    • 2018-05-30
    • 2016-10-06
    • 2016-06-03
    相关资源
    最近更新 更多