VB中通过outlook发送邮件时出错?

发布于 2025-01-05 23:32:43 字数 1239 浏览 2 评论 0原文

未处理的类型异常 “System.Runtime.InteropServices.COMException”发生在 myprogram.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("[email protected]")
        OutlookMessage.Subject = "Sending through Outlook"
        OutlookMessage.Body = "Testing outlook Mail"
        OutlookMessage.Send()
        OutlookMessage = Nothing
        AppOutlook = Nothing

    End Sub

错误出现在第 7 行,其中显示:

将食谱调暗为 Outlook.Recipients = OutlookMes​​sage.Recipients

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

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
myprogram.exe

Additional information: Operation aborted (Exception from HRESULT:
0x80004004 (E_ABORT))

The following code is what caused the error:

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("[email protected]")
        OutlookMessage.Subject = "Sending through Outlook"
        OutlookMessage.Body = "Testing outlook Mail"
        OutlookMessage.Send()
        OutlookMessage = Nothing
        AppOutlook = Nothing

    End Sub

The error was found in line 7 where it says:

Dim Recipents As outlook.Recipients = OutlookMessage.Recipients

If it's not too complicated, is there a way to do this without outlook? Because what happens when the user doesnt have outlook installed? I need a way to send an email from my application if anyone can help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2025-01-12 23:32:43

我从来没有尝试过做你正在做的事情,但你的台词不应该

   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("[email protected]", "password")
    Smtp.EnableSsl = True
    Smtp.Port = 587
    Smtp.Host = "smtp.gmail.com"
    Dim Email As New MailMessage()
    Email.From = New MailAddress("[email protected]")
    Email.To.Add("[email protected]")
    Email.Subject = "Test Mail"
    Email.Body = "SMTP server test"
    Smtp.Send(Email)

但这对我来说感觉有点不对劲。我想我可能会尝试其中一种解决方案,并让用户有机会为您提供一些设置(如果情况并非如此)。

I have never tried to do what you are doing, but shouldn't your lines:

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

be reversed? So that your OutlookMessage isn't based on that new instance? Of course, yeah you have that issue with what if they don't have outlook installed.

I would think that short of getting email setting information from your user it may be somewhat difficult to get email to send in a way that will always work. Considering Port 25 blocking, and SMTP filtering etc. Even if you used settings that the user has entered for mail as listed in the control panel, so many people use web based email now I can't imagine that would work. I suppose you could use an email account of your own that you know will work to send.

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

But that feels a little bit wrong to me. I think I might try one of these solutions and give the user the opportunity to give you some settings if it isn't the case for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文