Use. NET (C #) to send e-mail learning manuals (with success stories)
1. Learn three ways to send mail
2. The example describes the use of the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis
3. How to set the SMTP server for native IIS
1. Learn three ways to send mail
First: Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
//Pass the remote SMTP service to send the mail, the network here indicates you want to use the long-range SMTP server.  
Second: client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;  
//via the native SMTP server to send the email, The Pickupdirectoryfromiis here indicates that your mail will be sent through the SMTP server of the native IIS. So if you use this item, be sure to set the address of the server you want to go to on the SMTP service. This is detailed below.  
Third: client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;  
// Indicates that the e-mail message is copied to the directory specified by System.Net.Mail.SmtpDeliveryMethod.PickupDirectorylocation. So that there are other programs to perform the sending of the message.  
2. The example describes using the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis delivers the message. The code for
(1) mail.aspx is as follows (paste directly):  
- <%@ page language="C #" autoeventwireup="true" codefile="Mail.aspx.cs" inherits="Mail"%> /c2>
- <! DOCTYPE HTML public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
- "http://www.w3.org/1999/xhtml" >
- "Server" >
- <title>mail to Users</title>
- <body>
- <form id="Form1" runat= "Server" >
- <div>
- <asp:label id="Label1" runat="server" text="Label" ></asp:Label>
- </div>
- </form>
- </body>
(2) The Mail.aspx.cs code is as follows:
Note: General companies are agents of the Internet. So if you use this item. You can only send messages to the intranet.
However, this does not mean that the item cannot send an external network message. But the reason for the agency blockade.
- Using System;
- Using System.Data;
- Using System.Configuration;
- Using System.Collections;
- Using System.Web;
- Using System.Web.Security;
- Using System.Web.UI;
- Using System.Web.UI.WebControls;
- Using System.Web.UI.WebControls.WebParts;
- Using System.Web.UI.HtmlControls;
- Using System.Net;
- Using System.Net.Mail;
- Public partial class Mail:System.Web.UI.Page
- {
- protected void Page_Load (object sender, EventArgs e)
- {
- //sendmail (sender, recipient, subject, content, host, sender nickname, password, attachment)
- SendMail ("[email protected]", "[email protected]", " keynote", "Mail content test", "exhj.yyhj.com.cn", "Sun Festival", " yyhj", "" ");
- }
- public void SendMail (string send, string recieve, String subject, String mailbody, String host, String uname, Stri ng pwd, String strfilename)
- {
- //Generate a client object that uses SMTP to send mail
- System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient ();
- //Generate a host IP
- //client. Port = 25; 587, 465, 995
- Client. host = host;
- //indicates not to authenticate with the default credentials of the currently logged-on user
- Client. useDefaultCredentials =true;
- //include user name and password
- if (uname! = "")
- {
- Client. Credentials = New System.Net.NetworkCredential (uname, PWD);
- }
- //Specify how to send e-mail.
- Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;
- //Send the message through the native SMTP server,
- //In fact, you can set the "host, sender nickname, password", because your IIS server is already set up. and intra-company email is not required to verify.
- System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage ();
- Message. To.add (recieve);
- Message. From = new System.Net.Mail.MailAddress (send, uname, System.Text.Encoding.UTF8);
- Message. Subject = Subject;
- Message. Body = Mailbody;
- //define the message body and encode the subject
- Message. bodyencoding = System.Text.Encoding.GetEncoding ("UTF-8");
- Message. subjectencoding = System.Text.Encoding.GetEncoding ("UTF-8");
- //Gets or sets a value that indicates whether the e-mail body is HTML.
- Message. isbodyhtml = false;
- //Specify message priority
- Message. priority = System.Net.Mail.MailPriority.High;
- //Add Attachments
- //system.net.mail.attachment data = new Attachment (@ "E:\9527\tubu\PA260445. JPG ", System.Net.Mime.MediaTypeNames.Application.Octet);
- if (strFileName! = "" "" && strfilename! = null)
- {
- Attachment data = new Attachment (strFileName);
- Message. Attachments.Add (data);
- }
- Try
- {
- //Send
- Client. Send (message);
- Label1.Text = "sent successfully!" ";
- }
- catch (System.Net.Mail.SmtpException ex)
- {
- Label1.Text ="Send failed:" + ex. Message;
- }
- }
- }
2. Describes using the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network delivers the message. &NBSP;
Use this item. Your computer must first be directly linked to the extranet. &NBSP;
then direct mail.aspx.cs in Client.deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; change to client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network; &NBSP;
Then set the &NBSP;
//SendMail (sender, recipient, subject, content, host, sender nickname, password, attachment) &NBSP;
SendMail ("[email protected]", "[email protected]", "keynote", "12.37 Mail Content", "smtp.163.com", "Loeley", "81859505", "" "); &NBSP;
Turn from: http://hi.baidu.com/lslyl/blog/item/ ba67366ef4202ddd80cb4afa.html