从.NET Windows应用程序发送电子邮件 [英] Sending Email from a .NET Windows App

查看:81
本文介绍了从.NET Windows应用程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的用户从我正在开发的Windows应用程序发送电子邮件

Visual Basic.NET 2003.我找到了一个向SMTP发送电子邮件的好例子
$ b使用SmtpMail类的$ b服务器。但是,使用它,似乎

用户必须在他们的计算机上安装IIS。难道没有一个类能够检测计算机上可用的任何邮件服务器并使用它吗?怎么做

我创建这个功能而无需用户添加任何其他程序或

更改任何以前定义的配置 - 只需使用他们的电脑'

电子邮件系统?


谢谢,

joe

I want my users to send emails from a Windows app that I am developing in
Visual Basic.NET 2003. I found a good example of sending email to a SMTP
server using the SmtpMail class. However, using this, it seems, that the
user must install IIS on their computer. Isn''t there a class that will
detect whatever mail server is available on a computer and use that? How do
I create this functionality without having the user add any other programs or
change any previously defined configurations - just use their computer''s
email system?

Thanks,
joe

推荐答案

您需要某处的SMTP服务器。邮件需要排队。如果您在发送邮件之前编程

退出,则该过程需要继续运行。


如何设置中央IIS / SMTP服务器?它仍然适用于

SmtpMail。


-A


" joe215" <乔**** @ discussions.microsoft.com>在消息中写道

新闻:C6 ********************************** @ microsof t.com ...
You need a SMTP server somewhere. Mail needs to be queued. If you program
exits before you mail is sent, the process needs to keep running.

How about setting up a central IIS / SMTP server? It will still work with
SmtpMail.

-A

"joe215" <jo****@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
我希望我的用户从我在Visual Basic.NET 2003中开发的Windows应用程序发送电子邮件。我找到了一个向a发送电子邮件的好例子。 SMTP
服务器使用SmtpMail类。但是,使用它,似乎
用户必须在他们的计算机上安装IIS。难道没有一个类可以检测计算机上可用的任何邮件服务器并使用它吗?如何

我创建此功能,而无需用户添加任何其他程序

更改任何以前定义的配置 - 只需使用他们的计算机的电子邮件系统?

谢谢,
I want my users to send emails from a Windows app that I am developing in
Visual Basic.NET 2003. I found a good example of sending email to a SMTP
server using the SmtpMail class. However, using this, it seems, that the
user must install IIS on their computer. Isn''t there a class that will
detect whatever mail server is available on a computer and use that? How
do
I create this functionality without having the user add any other programs
or
change any previously defined configurations - just use their computer''s
email system?

Thanks,
joe



2005年11月10日星期四09:45:02 -0800,joe215写道:
On Thu, 10 Nov 2005 09:45:02 -0800, joe215 wrote:
我希望我的用户从我在Visual Basic.NET 2003中开发的Windows应用程序发送电子邮件。我找到了一个向a发送电子邮件的好例子SMTP
服务器使用SmtpMail类。但是,使用它,似乎
用户必须在他们的计算机上安装IIS。


不正确。您的示例必须假设,因为通过

默认安装IIS会设置SMTP中继服务器。但是,您的

程序可以访问的任何SMTP服务器都足够了。

是否有一个类可以检测到任何可用的邮件服务器电脑和使用?


私人功能DetectSMTPServer(_

可选ByVal主机As String =" 127.0.0.1")_

As Boolean

Dim tcp As New System.Net.Sockets.TcpClient

Dim ServerExists As Boolean = False

试试

tcp.Connect(host,25)

ServerExists = True

Catch ex As Exception

ServerExists = False

最后

tcp.Close()

结束尝试

返回ServerExists

结束功能


如果在

指定位置有一个SMTP服务器应答,则此函数返回true。如果您没有指定,则它假设本地机器。

如何创建此功能而无需用户添加任何其他程序或
更改任何先前定义的配置 - 只需使用他们的电脑的电子邮件系统?
I want my users to send emails from a Windows app that I am developing in
Visual Basic.NET 2003. I found a good example of sending email to a SMTP
server using the SmtpMail class. However, using this, it seems, that the
user must install IIS on their computer.
Not true. Your example must be assuming that, since installing IIS by
default sets up an SMTP relay server. However, any SMTP server that your
program can reach will suffice.
Isn''t there a class that will
detect whatever mail server is available on a computer and use that?
Private Function DetectSMTPServer( _
Optional ByVal host As String = "127.0.0.1") _
As Boolean
Dim tcp As New System.Net.Sockets.TcpClient
Dim ServerExists As Boolean = False
Try
tcp.Connect(host, 25)
ServerExists = True
Catch ex As Exception
ServerExists = False
Finally
tcp.Close()
End Try
Return ServerExists
End Function

This function returns true if there''s an SMTP server answering at the
specified location. If you don''t specify, it assumes the local machine.
How do
I create this functionality without having the user add any other programs or
change any previously defined configurations - just use their computer''s
email system?




如果你真的想使用他们的电脑的电子邮件系统,你可能会看看MAPI中的
。这里有一些我发现使用Outlook发送电子邮件的代码:

Dim mOutLookApp As Outlook.Application

Dim mNameSpace As Outlook.NameSpace

Dim mItem As Outlook.MailItem

设置mOutLookApp =新的Outlook.Application

设置mNameSpace = mOutLookApp.GetNamespace(" MAPI" )

mNameSpace.Logon ,, False,True


设置mItem = mOutLookApp.CreateItem(olMailItem)

mItem.To = strEmailAddress

mItem.Subject ="这是给你的

mItem.HTMLBody = strMessage


mItem.Sensitivity = olConfidential

mItem.Importance = olImportanceHigh


mItem.Send

或者您可以尝试使用目标邮件主机作为地址<通过使用DNS查找

目标域的MX记录,发送给你的
。没有内置的课程可以做到这一点,但是有几个人已经写了这些。请参阅
http://www.google .com / search?q = vb.net + MX +查找


以下免费COM对象声称在内部进行MX查找,给出

a非常小的足迹。但它是COM:

http://www.visualbuilder.com/compone...id=360&devid=1

你可能会发疯并扫描当地人用于开放端口的局域网25.


或者你可以问用户他们是否知道他们的邮件服务器名称。

这可能是安全。也许使用它作为默认选项,如果其他方法

失败。



If you really want to use "their computer''s email system", you might look
into MAPI. Here''s some code I found that uses Outlook to send an email:

Dim mOutLookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mItem As Outlook.MailItem

Set mOutLookApp = New Outlook.Application
Set mNameSpace = mOutLookApp.GetNamespace("MAPI")
mNameSpace.Logon ,, False, True

Set mItem = mOutLookApp.CreateItem(olMailItem)
mItem.To = strEmailAddress
mItem.Subject = "This is for you"
mItem.HTMLBody = strMessage

mItem.Sensitivity = olConfidential
mItem.Importance = olImportanceHigh

mItem.Send
Or you could attempt to use the destination mail host for the address
you''re sending to, by using DNS to look up the MX record for the
destination domain. No built-in classes to do this simply, but a few
people have written them. See
http://www.google.com/search?q=vb.net+MX+lookup

The following free COM object claims to do the MX lookup internally, giving
a very small footprint. But it''s COM:

http://www.visualbuilder.com/compone...id=360&devid=1

You could go crazy and scan the local LAN for an open port 25.

Or you could just ask the user if they know their mail server''s name.
That''s probably safe. Maybe use that as a default option, if other methods
fail.


对Ross和Andrew回复我的发送电子邮件。题。让我

改进我的问题,特别是现在我已经回顾了我之前提到的SMTP

示例。


我使用的示例来自MS Book,101 Visual Basic.NET

Applications。它是本书中的应用程序#75。当我运行它时,我收到一条

消息,我的计算机上没有SMTP邮件服务器 - 这不是

true。回顾本书中的讨论,它说要将app.config中的

应用程序设置更改为指向服务器。 app.config

文件包含以下信息:


<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>

< configuration>

< appSettings>

< add key =" SmtpMail.SmtpServer" value =" 127.0.0.1" />

< / appSettings>

< / configuration>


所以,现在我的问题是如何找到SmtpServer的''value''(我假设是一个IP

地址)?而且,我将如何以编程方式执行此操作,以便我的用户可以在他们的计算机上安装我的程序,该程序可以确定他们的Smtp服务器的位置,以及电子邮件可以是从

应用程序发送的?


" Ross Presser"写道:
To Ross and Andrew who responded to my "Sending Email" question. Let me
refine my question, especially now that I have looked back at the SMTP
example that I mentioned previously.

The example that I am using comes from the MS Book, "101 Visual Basic.NET
Applications". It is Application #75 in the book. When I run it, I get a
message that I don''t have an SMTP Mail Server on my computer - which is not
true. Looking back at the discussion in the book, it says to change the
application setting in app.config to point to the server. The app.config
file contains the following information:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SmtpMail.SmtpServer" value="127.0.0.1"/>
</appSettings>
</configuration>

So, now my question is how do I find the ''value'' (which I assume is an IP
address) for the SmtpServer? And, how would I do this programmatically so
that my users can install my program on their computer, the program can
identify the location of their Smtp Server, and emails can be sent from the
app?

"Ross Presser" wrote:
2005年11月10日星期四09:45:02 -0800,joe215写道:
On Thu, 10 Nov 2005 09:45:02 -0800, joe215 wrote:
我希望我的用户发送电子邮件来自我在Visual Basic.NET 2003中开发的Windows应用程序。我找到了一个使用SmtpMail类向SMTP服务器发送电子邮件的好例子。但是,使用它,似乎
用户必须在他们的计算机上安装IIS。
I want my users to send emails from a Windows app that I am developing in
Visual Basic.NET 2003. I found a good example of sending email to a SMTP
server using the SmtpMail class. However, using this, it seems, that the
user must install IIS on their computer.



不正确。您的示例必须假设,因为通过
默认安装IIS会设置SMTP中继服务器。但是,您的
程序可以访问的任何SMTP服务器都足够了。



Not true. Your example must be assuming that, since installing IIS by
default sets up an SMTP relay server. However, any SMTP server that your
program can reach will suffice.

是否有一个类可以检测到的任何邮件服务器计算机并使用它?
Isn''t there a class that will
detect whatever mail server is available on a computer and use that?



私有功能DetectSMTPServer(_
可选的ByVal主机As String =" 127.0.0.1")_
As Boolean
Dim tcp As New System.Net.Sockets.TcpClient
Dim ServerExists As Boolean = False
尝试
tcp.Connect(host,25)
ServerExists = True
抓住ex Exception
ServerExists = False
tcp.Close()
结束尝试
返回ServerExists
结束功能

>如果在指定位置有SMTP服务器应答,则此函数返回true。如果您没有指定,它将采用本地计算机。



Private Function DetectSMTPServer( _
Optional ByVal host As String = "127.0.0.1") _
As Boolean
Dim tcp As New System.Net.Sockets.TcpClient
Dim ServerExists As Boolean = False
Try
tcp.Connect(host, 25)
ServerExists = True
Catch ex As Exception
ServerExists = False
Finally
tcp.Close()
End Try
Return ServerExists
End Function

This function returns true if there''s an SMTP server answering at the
specified location. If you don''t specify, it assumes the local machine.

如何创建此功能而无需用户添加任何其他程序或
更改任何以前定义的配置 - 只需使用他们的计算机的电子邮件系统?
How do
I create this functionality without having the user add any other programs or
change any previously defined configurations - just use their computer''s
email system?



如果你真的想使用他们的计算机的电子邮件系统,你可能会看看MAPI。这里有一些我发现使用Outlook发送电子邮件的代码:

Dim mOutLookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mItem As Outlook.MailItem

设置mOutLookApp =新的Outlook.Application
设置mNameSpace = mOutLookApp.GetNamespace(" MAPI")
mNameSpace.Logon ,, False,True
设置mItem = mOutLookApp.CreateItem(olMailItem)
mItem.To = strEmailAddress
mItem.Subject ="这是给你的
mItem.HTMLBody = strMessage
mItem.Sensitivity = olConfidential
mItem.Importance = olImportanceHigh

mItem.Send

或者您可以尝试使用目标邮件主机获取地址
您发送给,通过使用DNS查找目标域的MX记录。没有简单的内置类可以做到这一点,但有几个人已经写过了。请参阅
http://www.google .com / search?q = vb.net + MX +查找

以下免费COM对象声称在内部进行MX查找,因此占用空间非常小。但它是COM:

http://www.visualbuilder.com/compone...id=360&devid=1

你可能会发疯并扫描本地局域网是否公开端口25.

或者你可以问用户他们是否知道他们的邮件服务器名称。
这可能是安全的。如果其他方法失败,可以使用它作为默认选项。



If you really want to use "their computer''s email system", you might look
into MAPI. Here''s some code I found that uses Outlook to send an email:

Dim mOutLookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mItem As Outlook.MailItem

Set mOutLookApp = New Outlook.Application
Set mNameSpace = mOutLookApp.GetNamespace("MAPI")
mNameSpace.Logon ,, False, True

Set mItem = mOutLookApp.CreateItem(olMailItem)
mItem.To = strEmailAddress
mItem.Subject = "This is for you"
mItem.HTMLBody = strMessage

mItem.Sensitivity = olConfidential
mItem.Importance = olImportanceHigh

mItem.Send
Or you could attempt to use the destination mail host for the address
you''re sending to, by using DNS to look up the MX record for the
destination domain. No built-in classes to do this simply, but a few
people have written them. See
http://www.google.com/search?q=vb.net+MX+lookup

The following free COM object claims to do the MX lookup internally, giving
a very small footprint. But it''s COM:

http://www.visualbuilder.com/compone...id=360&devid=1

You could go crazy and scan the local LAN for an open port 25.

Or you could just ask the user if they know their mail server''s name.
That''s probably safe. Maybe use that as a default option, if other methods
fail.



这篇关于从.NET Windows应用程序发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆