查看: 777|回复: 3
|
VB.NET 如何 SEND MAIL?
[复制链接]
|
|
如题,我想问问如果我想用VB.NET来SEND EMAIL,
要如何做呢?
因为ASP.NET我们可以用 System.Web.Mail,
但是如果VB.NET的话就不能用以上的COMPONENT了.
有没有SIMPLE的CODE给我REFER呢?
谢谢 |
|
|
|
|
|
|
|
发表于 25-8-2006 09:36 PM
|
显示全部楼层
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
If ToAddress.Text = "" Or From.Text = "" Then
MsgBox("You must enter both To and From email addresses.")
Exit Sub
End If
' Use the StringBuilder class instead of traditional string concatenation.
' It is optimized for building strings because it is capable of modifying
' the underlying buffer instead of having to make a copy of the string for
' each concatenation.
Dim sb As New StringBuilder()
' Build the email message body.
sb.Append("The following email was sent to you from the Send Mail " & _
"sample application:")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("SUBJECT: ")
sb.Append(Trim(Subject.Text))
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("MESSAGE: ")
sb.Append(Trim(Body.Text))
sb.Append(vbCrLf)
' Creating a mail message is as simple as instantiating a class and
' setting a few properties.
Dim mailMsg As New MailMessage(From.Text.Trim, ToAddress.Text.Trim)
With mailMsg
If Not String.IsNullOrEmpty(CC.Text) Then
.CC.Add(New MailAddress(CC.Text.Trim))
End If
If Not String.IsNullOrEmpty(BCC.Text) Then
.Bcc.Add(New MailAddress(BCC.Text.Trim))
End If
.Subject = Subject.Text.Trim
.Body = sb.ToString
If Not IsNothing(arlAttachments) Then
Dim mailAttachment As Attachment
For Each mailAttachment In arlAttachments
.Attachments.Add(mailAttachment)
Next
End If
End With
' Set the SmtpServer name. This can be any of the following depending on
' your local security settings:
' a) Local IP Address (assuming your local machine's SMTP server has the
' right to send messages through a local firewall (if present).
' b) 127.0.0.1 the loopback of the local machine.
' c) "smarthost" or the name or the IP address of the exchange server you
' utilize for messaging. This is usually what is needed if you are behind
' a corporate firewall.
' Use structured error handling to attempt to send the email message and
' provide feedback to the user about the success or failure of their
' attempt.
Try
Dim client As New SmtpClient("OSSTravelMate.oss.com") '("smarthost")
client.Send(mailMsg)
Attachments.Items.Clear()
Attachments.Items.Add("(No Attachments)")
MessageBox.Show("Your email has been successfully sent!", _
"Email Send Status", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch exp As Exception
MessageBox.Show("The following problem occurred when attempting to " & _
"send your email: " & exp.Message, _
Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub |
|
|
|
|
|
|
|
楼主 |
发表于 29-8-2006 11:13 AM
|
显示全部楼层
谢谢你,但请问要 IMPORT 什么COMPONENT才能
Dim client As New SmtpClient("OSSTravelMate.oss.com") '("smarthost")??
我用的是vb.net(c#) 2003,不是2005,
所以我没有
using System.Net.Mail; 的component.
[ 本帖最后由 little_orc 于 29-8-2006 11:24 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 29-8-2006 12:35 PM
|
显示全部楼层
SmtpClient
----------
Goto IIS --> Default SMTP Virtual Server --> Domains, you will see there is a 'Domain Name' on right panel, use that as your SmtpClient.
One more thing, remember to do the following step, or else you are unable to send mail:
- right click on the Default SMTP Virtual Server(left panel) and choose 'Properties'
- goto 'Access' tab, click 'Relay' button, tick 'All except the list below'
You need to add the following references:
-----------------------------------------
System
System.Deployment
System.Drawing
System.ServiceProcess
试试看... |
|
|
|
|
|
|
| |
本周最热论坛帖子
|