Wayne's profile卑鄙的垃圾箱PhotosBlogLists Tools Help

Blog


    23/11/2005

    .net 2.0 下发送邮件的方式。 转自蝈蝈俊

     

    2005年10月1日 20:52

    在.Net Framework 1.x 我们需要使用 System.Web.Mail 命名空间下的类 来进行发送邮件,但是功能比较弱,比如你的邮件服务器需要验证才能发送邮件,在.net 1.1 中,需要用下面的代码来做额外配置。

    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
    , "1");
    mail.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendusername
    , "my_username_here");
    mail.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendpassword
    , "super_secret");

    .net 1.x 下发送邮件的方式请参考:
    http://blog.joycode.com/joy/archive/2004/01/14/11405.aspx

    .Net Framework 2.0 下,在 System.Net.Mail 命名空间中提供了对邮件操作的支持,他的功能更强大。比如你的邮件服务器需要验证才能发送邮件,代码就只需简单成如下:

    public static void SendSMTPEMail(string strSmtpServer, string strFrom,
    string strFromPass, string strto, string strSubject, string strBody) { System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer); client.UseDefaultCredentials = false; client.Credentials =
    new System.Net.NetworkCredential(strFrom, strFromPass); client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message =
    new MailMessage(strFrom, strto, strSubject, strBody); message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; client.Send(message); }

    我们可以通过修改 UseDefaultCredentials  Credentials  DeliveryMethod  等属性,方便的支持各种情况下发送邮件的方式。

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    Amandaofsummer wrote:
    路过= =
    23 Nov.

    Trackbacks

    The trackback URL for this entry is:
    http://waynebaby1981.spaces.live.com/blog/cns!45FE30D49807DF3C!118.trak
    Weblogs that reference this entry
    • None