. Several different mail delivery solutions under the net environment

Source: Internet
Author: User
Tags exit copy getstream log readline reference mailmessage server port
Resolve 1, send with Outlook
For example: I have seen the Ufida U8 manufacturing (demo), where the mail function is by calling the Outlook ActiveX components
Advantages: Simple Development
Disadvantage: Dependent on Outlook components, SMTP Mail Service

The code to send the message is as follows:

Private Sub Send ()
Dim Outobj as New outlook.application ()
Dim Item as Outlook.mailitemclass
Item = Outobj.createitem (0)
item.to = "Lihonggen0@163.com"
Item.Subject = "Hello"
Item.Body = "Hell"
ITEM.ATTACHMENTS.ADD ("C:\abc.txt")
Item.Send ()
End Sub

Reference: Develop solutions using Microsoft Outlook 2002 http://www.microsoft.com/china/msdn/library/dndotnetout2k2/html/odc_oldevsol.asp

2. Web development, referencing System.Web.Mail classes in asp.net
The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Windows 2000 Collaboration Data object) message component. Mail messages are delivered by means of an SMTP mail service or any SMTP server built into Microsoft Windows 2000. Classes in this namespace can be in asp.net or any managed application
MailAttachment provides properties and methods for constructing e-mail attachments.
MailMessage provides properties and methods for constructing e-mail messages.
SmtpMail provides properties and methods for sending messages using the Collaboration Data Object (CDOSYS) message component of Windows 2000.
Messages can be delivered through the SMTP mail service or any SMTP server that is built into Microsoft Windows 2000. Types in the System.Web.Mail namespace can be used in asp.net or in any managed application.
SMTP server settings, now some free mail providers are no longer providing SMTP service for all messages, when sending mail, need to authenticate user information, consider SMTP user authentication issues
If the SMTP server on the local computer, send fast, basically do not worry, if not the local server, then it is best not to send too much, one is the speed problem, the second is to send too many messages, the SMTP server may be considered spam and denial of service
The code is as follows:

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim Mailobj as New mailmessage ()
Dim SMTP as SmtpMail
' Define the name of the SMTP server
Smtp. SmtpServer = "SMTP. XXX.com "
' Define the sending address of the message
Mailobj.from = "Lihonggen@XXX.com"
' Define the receiving address of the message
mailobj.to = "AAA@XXX.com"
' Define a message's dark-sending address
Mailobj.bcc= "Aa@sina.com"
' Define CC addresses for messages
mailobj.cc = "Aaa@sina.com"
' Define the subject of the message
Mailobj.subject = "Subject"
' Define the body of the message
Mailobj.body = "message body!" "
' Messages are sent in HTML format
Mailobj.bodyformat = mailformat.html
' Defines the finite level of the message, which is set to high
Mailobj.priority = Mailpriority.high
' Attach an attachment to the sent message
MAILOBJ.ATTACHMENTS.ADD (New mailattachment ("C:\aa.doc"))
Smtp.send (Mailobj)
End Sub

3. Use System.Net.Sockets in developing Windows applications in vb.net or C #
Also based on SMTP protocol
Introduction of SMTP protocol
1, the client through the server's 25 port to establish TCP/IP connection
Server side: server.com simple Mail Transfer Service Ready
2. The client uses the "HELO" command to identify the sender
Client: HELO server.com
Server side: server.com
3, the client sent mail command, the server side with OK as a response to indicate ready to receive
Client: MAIL from: <A@B.com>
Server side: OK
4, the client sends the RCPT command to identify the recipient, server-side response is willing to accept mail for recipients
Client: RCPT to: <c@d.com>
Server side: OK
5. Send mail with command data after negotiation
Client: DATA
Server side: 354 Start mail input:end with <CRLF>.<CRLF>
6, the client to the end of the input content sent out together
Client: Subject: <CRLF>
Content <CRLF>
.<crlf>
7, the client uses the QUIT command to exit.
Client: QUIT
Server side: server.com closing transmission channel

Advantages: Can be based on the issue of their own components, using sockets we can do network programming development
Disadvantages: A relatively large number of programs,

The code to send the message is as follows:

Dim SendData as String
Dim Szdata as Byte ()
Dim CRLF as String
CRLF = "\ r \ n"

' Create a connection to server port 25
Dim Smtpserv as New tcpclient (TXTSMTP. Text, 25)
Lstlog. Items.clear ()

' Show server initial information
Dim NETSTRM as NetworkStream
NETSTRM = Smtpserv.getstream ()

Dim RDSTRM as New StreamReader (Smtpserv.getstream ())
If rdstrm.readline () <> "" Then lstlog. Items.Add (Rdstrm.readline ())
'
SendData = "HELO Server" + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Flag Sender
SendData = "MAIL from:" + "<" + Txtfrom. Text + ">" + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Flag recipient
SendData = "RCPT to:" + "<" + Txtto.text + ">" + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Ready to send content
SendData = "DATA" + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Send a Theme
SendData = "SUBJECT:" + txtsub. Text + CRLF
' Send content
SendData = SendData + txtmsg. Text + CRLF

' End send
SendData = SendData + "." + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Exit
SendData = "QUIT" + CRLF
Szdata = System.Text.Encoding.ASCII.GetBytes (Senddata.tochararray ())
Netstrm.write (szdata, 0, Szdata.length)
Lstlog. Items.Add (Rdstrm.readline ())

' Close the connection
Netstrm.close ()
Rdstrm.close ()
Lstlog. Items.Add ("Connection close")
Lstlog. Items.Add ("Send Success")

You can also refer to: Create a mail send component using C # (SMTP)
Http://www.aspcool.com/lanmu/browse1.asp?ID=968&bbsuser=csharp

4, IIS SMTP with the basic letter Components CDONTS
You do not have to download specifically, Microsoft has provided this component, as long as the installation of 2000,nt SMTP will have.
Advantages: Components are brought by the operating system itself
Disadvantage: Poor function, scalability is not strong

MyMail = CreateObject ("CDONTS"). NewMail ")
Mymail.from = * * * Letter Sender's mailbox
mymail.to = * * * Letter Recipient Mailbox
mymail.cc = * * * CC
MYMAIL.BCC = * * * BCC
Mymail.subject = * * * "Letter Theme
Mymail.body = * * * Letter Body
' Set priority, 0-unimportant, 1-general, 2-important.
Mymail.importance = 2
Mymail.send ()
MyMail = Nothing

5, the use of JMail components
JMail has the following characteristics:
(1) can send the attachment;
(2) Detailed log ability, easy to see the problem;
(3) Set the priority of sending mail;
(4) Support a variety of formats of mail delivery, such as HTML or TXT to send mail. This is a free component.
(5) BCC/(CC) CC/emergency letter delivery capability;
(6) The most important thing is--free components, do not have to send money, so very worthwhile to use.
Site: http://www.dimac.net/, current version is 4.3

Common Properties for JMail components:
Body Message Text
Logging Call log record for debug
Priority message priority, from 1 to 5
Sender Sender
IP address or name of the serveraddress SMTP server
Subject message Headers

Common methods for JMail components:
AddAttachment Specify attachment file
AddRecipient Join a recipient
ADDRECIPIENTBCC hidden copy cc, only senders and Bcc recipients know.
ADDRECIPIENTCC Copy cc
Execute send out message

After understanding the necessary properties and methods, the remainder receives the email input from the user as a parameter to the AddRecipient method, and then fills in the remaining attributes as needed, and finally sends it in the Execute method. For example:

Dim JMail
JMail = Server.CreateObject ("Jmail.smtpmail")
jmail.logging = True
jmail.serveraddress = "202.96.144.48"
Jmail.sender = "Lihonggen0@163.com"
Jmail.subject = "Subject."
Jmail.body = "body."
Jmail.addrecipient ("bbbb@163.com")
Jmail.addattachment ("C:\go.gif")
Jmail.priority = 3
Jmail.execute ()
JMail = Nothing

Summary: Choose which scheme, depending on the application and requirements of the program, this article examples of several programs for everyone's reference. For example, Ufida U8, call the Outlook components, the same integration into their own software. I write the management system, write their own components (SMTP), the function is equally strong, but coding time is long!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.