XuGang

记录一个程序员的成长

 

VS2005发送电子邮件(转)

/// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="sendTo"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    /// <returns></returns>
    public static bool SendEmail(string sendTo,string subject,string body)
    {
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        client.Host = "smtp.163.com";
        client.UseDefaultCredentials = false;
        //client.Credentials = new System.Net.NetworkCredential("发件人邮箱", "******");
        client.Credentials = new System.Net.NetworkCredential("你的邮箱", "******");
        //星号改成自己邮箱的密码
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("你的邮箱", sendTo);
        message.Subject = subject;
        message.Body = body;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.IsBodyHtml = false;
        //添加附件
        //Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
        //message.Attachments.Add(data);

        try
        {
            client.Send(message);
            return true;
            //MessageBox.Show("Email successfully send.");
        }
        catch (Exception ex)
        {
            return false;
            //MessageBox.Show("Send Email Failed." + ex.ToString());
        }

    } 


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1685190

posted on 2007-07-11 15:06  钢钢  阅读(448)  评论(0编辑  收藏  举报

导航